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
RE: [Caml-list] @, List.append, and tail recursion
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-01-31 (09:34)
From: Christophe Raffalli <Christophe.Raffalli@u...>
Subject: Re: [Caml-list] @, List.append, and tail recursion wrote:
> Thanks for the reference. I get nailed by this a couple of times a 
> year, and I usually just blame myself for being sloppy and recode 
> all of my maps, appends, and filters to remove it. I think it would 
> be great if those three functions (at least) could be tail recursive, 
> either by having the compiler do it or by providing these "hole 
> abstractions" in the language. 
> I wonder, like the other Brian, if there are existing ML compilers which 
> implement this optimization, and if there is any chance we'll see this in 
> OCaml?
> On Fri, 24 Jan 2003, Andrew Kennedy wrote:

To compute append l1 l2 in a tail recursive way the only solution I see 
(in a pure functionnal program) is to traverse l1 twice (to reverse it 
first), which replaces using memory on the stack by using memory to 
build an intermediate list (and this use more memory ?).

Usually in recursive program, when you limit youself to tail recursive 
function, you have often to reverse list and lisp languages always 
provide the rev_append function which is tail recursive and very often 
what you need (often l1 is an accumulator and is naturaly reversed 
compared with l2).

May be rev_append could be added to the list module (despite the fact it 
is trivial to write it yoursleft) ?

>>The optimization you describe is sometimes known as
>>"tail modulo cons", and is an example of "destination-passing
>>style". In other words, the place to put the result (in
>>this case, the address of the tail of a just-constructed 
>>cons cell) is passed on in a tail-recursive call.
>>See "A Functional Representation of Data Structures with a Hole"
>>by Minamide in POPL'98.
>>Although Minimide formalizes the problem in the context of
>>a typed intermediate language, it's probably quite easy to 
>>spot special cases quite far down the compiler pipeline.
>>- Andrew.
>>>-----Original Message-----
> [...snip...]
> -------------------
> To unsubscribe, mail Archives:
> Bug reports: FAQ:
> Beginner's list:

Christophe Raffalli
Université de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tél: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
IMPORTANT: this mail is signed using PGP/MIME
At least Enigmail/Mozilla, mutt or evolution
can check this signature