Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Preferred Way to Split a List
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alain Frisch <alain@f...>
Subject: Re: [Caml-list] Preferred Way to Split a List
skaller wrote:
> The fastest way is:

How can you be so sure? Do you have a proof or some intuitive argument?
Or maybe you tried all the possible other implementations? The following 
should not be that much slower (I did not try it, though, and of course, 
it is not tail-rec):

let split l n =
   let l2 = ref [] in
   let rec aux l n =
     if n = 0 then (l2 := l; [])
     else match l with
       | [] -> []
       | hd :: tl -> hd :: (aux tl (n - 1))
   let l1 = aux l n in
   (l1, !l2)