Version française
Home     About     Download     Resources     Contact us    
Browse thread
OCaml troll on Slashdot
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Karl Zilles <zilles@1...>
Subject: Re: [Caml-list] tail-recursion vs. no tail-recursion in list functions
Oliver Bandel wrote:
> On Thu, Mar 17, 2005 at 12:31:57PM +0100, sebastian.egner@philips.com wrote:
>>let unbreakable_map f xs =
>>    let rec 
>>      map limit f xs = (* recursion depth limited to limit *)
>>        match xs with
>>          []                   -> []
>>        | x::xs when limit > 0 -> let f_x = f x in f_x::(map (limit-1) f 
>>xs)
>>        | _                    -> List.rev_append (List.rev_map f xs) []
>>    in  map 512 f xs;;
>>
>>The function is not tail-recursive for lists of length up to 512---at 
>>which point it
>>switches to a tail-recursive algorithm and uses the heap instead of the 
>>stack
>>to keep track of structural recursion.
> 
> IMHO this does not makes sense.
> Better checking listlength with List.length and then calling the
> needed function directly.
> Why using an integer counter... this introduces overhead.

Well, if you use List.length, then you're running an integer counter 
over the entire length of the list.  His way, you only count the first 
512 items.

However, given the weird benchmark timings, God knows which would be 
faster :)