Version française
Home     About     Download     Resources     Contact us    
Browse thread
Newbie list question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Joel Christner <joel.christner@g...>
Subject: Newbie list question
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