caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Newbie list question
@ 2009-05-13 17:24 Joel Christner
  2009-05-13 18:06 ` [Caml-list] " Cedric Auger
  2009-05-15 13:49 ` Florian Hars
  0 siblings, 2 replies; 4+ messages in thread
From: Joel Christner @ 2009-05-13 17:24 UTC (permalink / raw)
  To: caml-list, Joel Christner

[-- 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 --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-05-15 13:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-13 17:24 Newbie list question Joel Christner
2009-05-13 18:06 ` [Caml-list] " Cedric Auger
2009-05-13 20:24   ` John Li
2009-05-15 13:49 ` Florian Hars

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).