From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id VAA09130; Fri, 30 Apr 2004 21:01:20 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id VAA09524 for ; Fri, 30 Apr 2004 21:01:19 +0200 (MET DST) Received: from rabelais.socialtools.net (rabelais.socialtools.net [81.2.94.243]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i3UJ1ISH013984 for ; Fri, 30 Apr 2004 21:01:18 +0200 Received: by rabelais.socialtools.net (Postfix, from userid 108) id D7661232DD; Fri, 30 Apr 2004 20:01:17 +0100 (BST) Received: from socialtools.net (chaucer.socialtools.net [81.2.94.242]) by rabelais.socialtools.net (Postfix) with ESMTP id 09877232DB; Fri, 30 Apr 2004 20:01:17 +0100 (BST) Message-ID: <4092A27C.1010305@socialtools.net> Date: Fri, 30 Apr 2004 20:01:16 +0100 From: Benjamin Geer User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040113 X-Accept-Language: en-gb, en, fr, it MIME-Version: 1.0 To: Rahul Siddharthan Cc: caml-list@inria.fr Subject: Re: [Caml-list] "List.index" or "List.unique" functions? References: <20040430175429.GB11118@online.fr> In-Reply-To: <20040430175429.GB11118@online.fr> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Checker-Version: SpamAssassin 2.63 (2004-01-11) on rabelais.socialtools.net X-Spam-Status: No, hits=-4.9 required=5.0 tests=BAYES_00 autolearn=ham version=2.63 X-Miltered: at concorde with ID 4092A27E.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 elem:01 elem:01 match:02 wrote:03 suppose:03 let:04 let:04 functions:05 fold:07 benjamin:07 index:91 sort:11 create:13 rev:13 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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]) Here's one way: let unique ls = let uniq_aux new_ls old_elem = match new_ls with | [] -> [ old_elem ] | hd :: tl when hd = old_elem -> new_ls | _ -> old_elem :: new_ls in let sorted = List.sort compare ls in List.rev (List.fold_left uniq_aux [] sorted) ;; Ben ------------------- 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