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 TAA05530; Fri, 30 Apr 2004 19:54:34 +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 TAA05402 for ; Fri, 30 Apr 2004 19:54:33 +0200 (MET DST) Received: from papagena.rockefeller.edu (papagena.rockefeller.edu [129.85.41.71]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i3UHsWSH005344 for ; Fri, 30 Apr 2004 19:54:32 +0200 Received: from papagena.rockefeller.edu (localhost.localdomain [127.0.0.1]) by papagena.rockefeller.edu (8.12.8/8.12.8) with ESMTP id i3UHsTQi011228 for ; Fri, 30 Apr 2004 13:54:30 -0400 Received: (from rsidd@localhost) by papagena.rockefeller.edu (8.12.8/8.12.8/Submit) id i3UHsTlI011226 for caml-list@inria.fr; Fri, 30 Apr 2004 13:54:29 -0400 Date: Fri, 30 Apr 2004 13:54:29 -0400 From: Rahul Siddharthan To: caml-list@inria.fr Subject: [Caml-list] "List.index" or "List.unique" functions? Message-ID: <20040430175429.GB11118@online.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4.1i X-Operating-System: Linux 2.4.20-20.9smp i686 X-Miltered: at concorde with ID 409292D8.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; grail:01 python:01 python:01 ocaml:01 ocaml:01 rec:01 rec:01 concise:01 concise:01 suppose:03 elegant:04 let:04 let:04 cleaner:04 ugly:05 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk I just discovered OCaml a week or so ago, and it really seems to be the "holy grail" -- more concise and elegant that python, almost as fast as C. I wish I'd known of it a year ago. Now I just need to get used to the functional way of thinking... 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]) In python, I can define a function to do this quite concisely, eg: unique = lambda l: [l[n] for n in range(len(l)) if l.index(l[n])==n] How do I do it in OCaml? Are there functions equivalent to index (return the position of the first matching element in the list) and range (range n = [0;1;...;n-1]), or is there a cleaner way to do it? The best I can come up with is: let unique l = let range n = let rec rangen n lacc = if n<0 then lacc else rangen (n-1) (n::lacc) in rangen (n-1) [] in let index a l = let rec indexn a l n = if n==(List.length l) then -1 else if (List.nth l n) =a then n else indexn a l (n+1) in indexn a l 0 in List.map (fun n -> List.nth l n) (List.filter (fun n -> n=(index (List.nth l n) l)) (range (List.length l)));; (it would be more concise if range and index already exist, but even then, the last line looks rather ugly to me...) Thanks Rahul ------------------- 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