From: Brian Hurt <bhurt@janestcapital.com>
To: skaller <skaller@users.sourceforge.net>
Cc: Alain Frisch <alain@frisch.fr>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Preferred Way to Split a List
Date: Tue, 30 Oct 2007 09:05:32 -0400 [thread overview]
Message-ID: <47272C1C.4060009@janestcapital.com> (raw)
In-Reply-To: <1193742901.6697.5.camel@rosella.wigram>
[-- Attachment #1: Type: text/plain, Size: 1543 bytes --]
skaller wrote:
>On Tue, 2007-10-30 at 08:58 +0100, Alain Frisch wrote:
>
>
>>skaller wrote:
>>
>>
>>>The fastest way is:
>>>
>>>
>>How can you be so sure?
>>
>>
>
>Why do I need to be something naturally impossible?
>Certainty isn't even possible in the presence of a proof.
>
>The fact here is I made a complete mess!
>
>My algorithm doesn't meet the specification. LOL!
>It doesn't split a list in two dang it! It actually
>splits it in two then recombines the result and
>returns the original list! :)
>
>
>
>
Just so people know, Obj.magic is not necessary here:
let take n lst =
let rec loop accum n lst =
if n == 0 then
List.rev accum
else match lst with
| x :: xs -> loop (x :: accum) (n - 1) xs
| [] -> List.rev accum
in
if n < 0 then
invalid_arg "Negative argument to List.take"
else
loop [] n lst
;;
let rec drop n lst =
if n < 0 then
invalid_arg "Negative argument to List.drop"
else if n == 0 then
lst
else match lst with
| _ :: xs -> drop (n - 1) xs
| [] -> []
;;
I'd also take a look at the standard library functions List.filter and
List.partition. With their standard, Obj.magic-less, implementations.
If the list is long enough that the extra overhead of calling List.rev
is a problem, I'd recommend using a better data structure than a list.
I'm fond of tree-based "functional arrays", which allow for splitting
them in O(log N), instead of O(N). We're clock cycle tuning bubble sort
here.
Brian
[-- Attachment #2: Type: text/html, Size: 2603 bytes --]
next prev parent reply other threads:[~2007-10-30 13:05 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-10-29 23:33 Robert Fischer
2007-10-30 0:45 ` [Caml-list] " Matthew William Cox
2007-10-30 13:18 ` Robert Fischer
2007-11-03 3:06 ` Nathaniel Gray
2007-10-30 1:20 ` Julien Moutinho
2007-10-30 1:38 ` Karl Zilles
2007-10-30 5:46 ` skaller
2007-10-30 7:58 ` Alain Frisch
2007-10-29 9:34 ` Christophe Raffalli
2007-10-30 11:31 ` skaller
2007-10-30 12:30 ` David Allsopp
2007-10-30 15:03 ` Christophe Raffalli
2007-10-30 11:15 ` skaller
2007-10-30 13:05 ` Brian Hurt [this message]
2007-10-30 7:50 ` Jon Harrop
2007-10-30 13:20 ` Robert Fischer
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=47272C1C.4060009@janestcapital.com \
--to=bhurt@janestcapital.com \
--cc=alain@frisch.fr \
--cc=caml-list@yquem.inria.fr \
--cc=skaller@users.sourceforge.net \
/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