caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Joel Christner <joel.christner@gmail.com>
To: caml-list@yquem.inria.fr, Joel Christner <joel.christner@gmail.com>
Subject: Newbie list question
Date: Wed, 13 May 2009 10:24:33 -0700	[thread overview]
Message-ID: <9da743ed0905131024u53a207f0s9e963be782f3f2c6@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1657 bytes --]

Hello,

I posted this question on a couple of newsgroups as well, please pardon the
cross-posting and the newbie question.

What I'm wanting to do is create a ref list that contains a series of
strings, i.e.
# let varlist = ref [];;
# let a = "a";;
# let b = "b";;
# let c = "c";;

Then throughout the course of program operation, I will be adding data
into this list, i.e.:
# varlist.contents <- a::varlist.contents;;
# varlist.contents <- b::varlist.contents;;
# varlist.contents <- c::varlist.contents;;

Which is working fine, i.e.
# varlist.contents;;
- : string list = ["c"; "b"; "a"]

But what I'm trying to do next I can't figure out how to get working.
What I'd like to do is have a function that first checks to see if the
item is already in the ref list.  If it is, do nothing.  If it isn't,
add it.  Here's what I tried, which is of course failing.  If anyone has
a suggestion on how to make it work would you please help me out?

# let rec addvariable stringdata listname =
  match listname.contents with
  | [] -> (listname.contents <- stringdata::listname.contents); ()
  | [a] -> if a.contents = stringdata then () else (listname.contents <-
stringdata::listname.contents); ()
  | h::t -> if h.contents = stringdata then () else addvariable
stringdata t.contents
;;

Which returns...
This expression has type 'a ref but is here used with type 'a
#

Does anyone have any suggestions on how to change the above to make it
work?  Basically I want the function to either 1) add the contents to
the list if no duplicates exist and return unit or 2) return unit if it
determines that the string is already there.

Thanks for any and all help
Joel

[-- Attachment #2: Type: text/html, Size: 1930 bytes --]

             reply	other threads:[~2009-05-13 17:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-13 17:24 Joel Christner [this message]
2009-05-13 18:06 ` [Caml-list] " Cedric Auger
2009-05-13 20:24   ` John Li
2009-05-15 13:49 ` Florian Hars

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9da743ed0905131024u53a207f0s9e963be782f3f2c6@mail.gmail.com \
    --to=joel.christner@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).