From: Martin Jambon <martin_jambon@emailuser.net>
To: Rahul Siddharthan <rsidd@online.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] "List.index" or "List.unique" functions?
Date: Sat, 1 May 2004 02:51:17 +0800 (HKT) [thread overview]
Message-ID: <Pine.LNX.4.44.0405010206150.4692-100000@localhost> (raw)
In-Reply-To: <20040430175429.GB11118@online.fr>
On Fri, 30 Apr 2004, Rahul Siddharthan wrote:
> 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])
1) Naive O(n^2) solution:
let rec unique = function
[] -> []
| hd :: tl ->
if List.mem hd tl then unique tl
else hd :: unique tl
The result is not sorted.
2) With a hash table you can get something quite efficient (O(n)) and not
too difficult to write:
let unique l =
let tbl = Hashtbl.create 10 in
List.iter (fun i -> Hashtbl.replace tbl i ()) l;
Hashtbl.fold (fun key data accu -> key :: accu) tbl []
The result is not sorted.
You can replace "10" with "List.length l" if really you don't have any
idea of the initial size of the table and want to avoid multiple resizings
of the table.
"fold" functions (List.fold_left, List.fold_right, Hashtbl.fold,
Array.fold_left...) are very useful, and are most of the time more
appropriate than imperative loops ("for" and "while").
3) With sort/simplify (O(n log n)) (I expect it to be much less
efficient than 2)):
let unique l =
let rec simplify last l =
match l with
[] -> [last]
| hd :: tl ->
if hd = last then
simplify last tl
else
last :: simplify hd tl in
match List.sort compare l with
[] -> []
| hd :: tl ->
simplify hd tl
Martin
-------------------
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
next prev parent reply other threads:[~2004-04-30 18:51 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-04-30 17:54 Rahul Siddharthan
2004-04-30 18:51 ` Martin Jambon [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.44.0405010206150.4692-100000@localhost \
--to=martin_jambon@emailuser.net \
--cc=caml-list@inria.fr \
--cc=rsidd@online.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox