caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Daniel de Rauglaudre <daniel.de_rauglaudre@inria.fr>
To: "Alexander V. Voinov" <avv@quasar.ipa.nw.ru>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] Generation of streams is slow
Date: Sat, 14 Jul 2001 10:31:10 +0200	[thread overview]
Message-ID: <20010714103110.A26003@verdot.inria.fr> (raw)
In-Reply-To: <3B4FC748.F9E14008@quasar.ipa.nw.ru>; from avv@quasar.ipa.nw.ru on Fri, Jul 13, 2001 at 09:15:04PM -0700

Hi,

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
either.

> 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.

-- 
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
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


  parent reply	other threads:[~2001-07-14  8:31 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-13 22:06 Alexander V. Voinov
2001-07-14  2:27 ` Daniel de Rauglaudre
2001-07-14  2:51   ` Alexander V. Voinov
2001-07-14  4:04     ` Daniel de Rauglaudre
2001-07-14  4:15       ` Alexander V. Voinov
2001-07-14  4:40         ` Alexander V. Voinov
2001-07-14  8:34           ` Daniel de Rauglaudre
2001-07-14 18:08             ` Alexander V. Voinov
2001-07-14 19:02               ` Daniel de Rauglaudre
2001-07-14 19:51                 ` Alexander V. Voinov
2001-07-14 21:59                 ` Alexander V. Voinov
2001-07-14 22:29                   ` Daniel de Rauglaudre
2001-07-17  2:36               ` Jacques Garrigue
2001-07-14  8:31         ` Daniel de Rauglaudre [this message]
2001-07-20  7:49           ` Chris Hecker
2001-07-17 16:01 Dave Berry

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20010714103110.A26003@verdot.inria.fr \
    --to=daniel.de_rauglaudre@inria.fr \
    --cc=avv@quasar.ipa.nw.ru \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).