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: Oliver Bandel <oliver@f...>
Subject: Re: [Caml-list] tail-recursion vs. no tail-recursion in list functions
On Thu, Mar 17, 2005 at 12:31:57PM +0100, sebastian.egner@philips.com wrote:
> Just to chime in on this...
> 
> Did anybody every consider the following simple solution for the 'map' 
> problem?
> 
> 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.

Better use the match on limit and then call one of the
functions (which both are without a limit-incrementor).

BTW: As stated out by other people, the needed limit can vary.
     so it should be possible to give it as argument (at least
     as an optional value).


Ciao,
   Oliver