« Return to Thread: tail recursion

Re: "ocaml_beginners"::[] tail recursion

by Fabrice Marchant-2 :: Rate this Message:

Reply to Author | View in Thread

On Mon, 10 Mar 2008 11:52:19 -0700
Karl Zilles <zilles@...> wrote:

> let rec rev_append l1 l2 =
>
>    match l1 with
>
>    | [] -> l2
>    | hd::tl -> rev_append tl (hd::l2);;

  Yes, it's list.ml source.

I do not say the following code is better, but jff throw away the two parameters lists :

let id x = x

let cons a d = a::d

let ( <<- ) f g x = f (g x)

let append =
  let rec rappend = function
      [] -> id
    | h::t -> rappend t <<- (cons h) in
    rappend <<- List.rev

  It is the opportunity to ask this :
I wonder if this later is perfectly equivalent to the former ?
Is it absolutely true that the 3 tiny functions id, cons and ( <<- ) will be inlined without any efficiency loss ?

  Thanks,

Fabrice

 « Return to Thread: tail recursion