Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] "List.index" or "List.unique" functions?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-04-30 (19:09)
From: Karl Zilles <zilles@1...>
Subject: Re: [Caml-list] "List.index" or "List.unique" functions?
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.

You might want to check out the mailing 
list.  That is a good place to ask questions like this.

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: