English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Generation of streams is slow
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-07-14 (08:31)
From: Daniel de Rauglaudre <daniel.de_rauglaudre@i...>
Subject: Re: [Caml-list] Generation of streams is slow

On Fri, Jul 13, 2001 at 09:15:04PM -0700, Alexander V. Voinov wrote:

> I see, Prolog habits are hard to leave. But this solution doesn't look
> as "natural" as mine

It is not, indeed, but if you have long lists, it is the normal approach.
You would have the same problem with List.map which is not tail recursive

> and requires additional time to reverse the result.

Exact, but List.rev is tail recursive.

All non tail recursive functions can be rewritten in a recursive way
by adding the accumulator value as parameter. If if is a list, it is
reversed and you have to reverse it.

A way to avoid that (I mean creation of a reversed list and reversion)
is to use mutable types, what the list type is not.

> I try to acquire hints for the everyday use, of lists and other
> abstract sequences in this case. Not just a particular problem to solve.

In the List module, some functions are tail recursive, other not. It has
been planed for non too big lists. If you have big lists, you have to be
careful of that. You may have to write your own functions in some cases.

Also be careful of code enclosed by try..with: it prevents tail
recursion. A "try a with b -> c" enclosing a recursive call in "a" must
be rewritten as:

    match (try Some a with b -> None) with
      Some x -> a
    | None -> c

It is heavy code, but mandatory: I often use it.

Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr