caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christophe Raffalli <Christophe.Raffalli@univ-savoie.fr>
To: Alain Frisch <alain@frisch.fr>
Cc: skaller <skaller@users.sourceforge.net>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Preferred Way to Split a List
Date: Mon, 29 Oct 2007 10:34:13 +0100	[thread overview]
Message-ID: <4725A915.3020005@univ-savoie.fr> (raw)
In-Reply-To: <4726E433.7060408@frisch.fr>


[-- Attachment #1.1: Type: text/plain, Size: 1043 bytes --]

Or

let split l n =
  let rec aux acc l n =
     if n = 0 then List.rev acc, l
     else match l with
        [] -> List.rev acc, []
     | hd::l -> aux (hd::acc) l (n-1)
  in aux [] l n

This is tail recursive, it does not use magic nor  references
and keed the original ordering of the list.

Finally, I did in the past some testing  comparing a tail-rec append or map
using magic and the composition of rev and rev-append or rev-map ...
There was no noticable difference, because the extra cost  of
mutability (regarding the managment of the grey val list) compensate
the extra call to reverse.

So I think I would write split the way shown above if ordering matters.

Cheers,
Christophe
>
> Alain
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

[-- Attachment #1.2: Christophe.Raffalli.vcf --]
[-- Type: text/x-vcard, Size: 380 bytes --]

begin:vcard
fn:Christophe Raffalli
n:Raffalli;Christophe
org:LAMA (UMR 5127)
email;internet:Christophe.Raffalli@univ-savoie.fr
title;quoted-printable:Ma=C3=AEtre de conf=C3=A9rences
tel;work:+33 4 79 75 81 03
note;quoted-printable:Message sign=C3=A9 cryptographiquement avec pgp, la clef publique est sur=
	 subkeys.pgp.net
x-mozilla-html:TRUE
version:2.1
end:vcard


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 252 bytes --]

  reply	other threads:[~2007-10-30  9:31 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 [this message]
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
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=4725A915.3020005@univ-savoie.fr \
    --to=christophe.raffalli@univ-savoie.fr \
    --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;
as well as URLs for NNTP newsgroup(s).