From: "Laurent Chéno" <laurent.cheno@noos.fr>
To: Caml-list <caml-list@margaux.inria.fr>
Subject: Re: [Caml-list] Queens examples
Date: Sun, 26 Aug 2001 15:57:45 +0200 [thread overview]
Message-ID: <200108261357.f7QDvjj02557@concorde.inria.fr> (raw)
In-Reply-To: <0108261423530A.02716@ice>
(* please excuse my poor english *)
Le dimanche 26 août 2001, à 01:33, Gerd Stolpmann a écrit :
> For example, look at the definition of concmap:
>
> let rec concmap f = function
> | [] -> []
> | h :: t -> f h (concmap f t);;
>
> The point is that the recursive call (concmap f t) occurs within an
> expression,
> and the current execution environment must be saved on the stack before
> the
> self-invocation such that it is still available when the containing
> expression
> (f h (concmap f t)) is being evaluated. This is what all programming
> languages
> do when recursive definitions are executed.
>
> You can avoid that only by changing the algorithm, i.e. avoid concmap
> (and
> perhaps filter_append). The simplest way is to make some parts of the
> program
> imperative, because managing the stack effectively can be _very_
> difficult for
> purely functional programming.
You can avoid the stack problem by writing (still functionnaly) :
let concmap f l =
let rec aux result = function
| [] -> result
| h :: t -> aux (f h result) t
in aux [] (List.rev l) ;;
Of course, List.rev has been written with terminal recursion, like this :
let reverse l =
let rec aux result = function
| [] -> result
| h :: t -> aux (h :: result) t
in aux [] l ;;
(compare the two functions... !)
Best regards,
Laurent
-------------------
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
next prev parent reply other threads:[~2001-08-26 13:57 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-08-26 7:25 Al Christians
2001-08-26 11:33 ` Gerd Stolpmann
2001-08-26 13:57 ` Laurent Chéno [this message]
2001-08-27 10:53 ` Frank Atanassow
2001-08-27 15:25 ` Xavier Leroy
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=200108261357.f7QDvjj02557@concorde.inria.fr \
--to=laurent.cheno@noos.fr \
--cc=caml-list@margaux.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