caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] "List.index" or "List.unique" functions?
@ 2004-04-30 17:54 Rahul Siddharthan
  2004-04-30 18:51 ` Martin Jambon
                   ` (3 more replies)
  0 siblings, 4 replies; 27+ messages in thread
From: Rahul Siddharthan @ 2004-04-30 17:54 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2004-05-02 15:59 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-30 17:54 [Caml-list] "List.index" or "List.unique" functions? Rahul Siddharthan
2004-04-30 18:51 ` Martin Jambon
2004-04-30 19:01 ` Benjamin Geer
2004-04-30 19:07   ` Thanks " Rahul Siddharthan
2004-04-30 19:08 ` Karl Zilles
2004-04-30 19:29   ` Matthieu Sozeau
2004-04-30 20:01     ` Karl Zilles
2004-04-30 20:05   ` Remi Vanicat
2004-04-30 20:47     ` JM Nunes
2004-04-30 20:58       ` Karl Zilles
2004-05-01  1:59   ` [Caml-list] List.rev skaller
2004-05-01  4:18     ` Jon Harrop
2004-05-01  4:38     ` brogoff
2004-05-01  5:12       ` skaller
2004-05-01  7:08         ` William Lovas
2004-05-01  8:10           ` skaller
2004-05-01  8:32             ` Jon Harrop
2004-05-01  9:24               ` skaller
2004-05-02 12:07             ` Andreas Rossberg
2004-05-02 13:29               ` skaller
2004-05-01 10:07         ` Richard Jones
2004-05-01 10:09           ` Nicolas Cannasse
2004-05-02 16:04             ` Brian Hurt
2004-05-01 10:32           ` Jon Harrop
2004-05-01 16:41     ` John Goerzen
2004-05-01 19:11       ` skaller
2004-05-01 10:03 ` [Caml-list] "List.index" or "List.unique" functions? Richard Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).