Browse thread
[Caml-list] "List.index" or "List.unique" functions?
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Remi Vanicat <remi.vanicat@l...> |
| Subject: | Re: [Caml-list] "List.index" or "List.unique" functions? |
Karl Zilles <zilles@1969.ws> writes: > Rahul Siddharthan wrote: >> I have a question: suppose I have a list l1, and I want to create a new >> list l2 with only one copy of any repeated members of the first list >> (eg, l1=[1;2;3;4;3;4;5;6;5] -> l2=[1;2;3;4;5;6]) > > let unique l = List.rev (List.fold_left (fun results x -> if List.mem > x results then results else x::results) [] l);; > > unique [1;2;3;4;3;4;5;6;5];; > - : int list = [1; 2; 3; 4; 5; 6] > > List.rev is not tail recursive, so you wouldn't want to use this > function on industrial size lists. List.rev is tail recursive... [...] -- Rémi Vanicat ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners