Version française
Home     About     Download     Resources     Contact us    
Browse thread
OCaml and tail recursion
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alain Frisch <frisch@c...>
Subject: Re: OCaml and tail recursion
On Mon, 13 Dec 1999, Norman Ramsey wrote:

> [snip]
> Apparently ocamlc doesn't optimize tail calls.

Yes it does. It is difficult to help if you don't give the code or its
structure. If you build a list with the characters you parse, consider
rewriting a code like :

let rec map f = function
 | h::t -> (f h)::(m f t)
 | _ -> []

(which is not tail recursive)

into something like:

let rec map f l =
 let rec aux accu = function
 | h::t -> aux ((f h)::accu) t
 | _ -> []
 in
 List.rev (aux [] l)

(List.rev is tail recursive)


Alain