From: "David McClain" <dmcclain@azstarnet.com>
To: "Juan Jose Garcia Ripoll" <jjgarcia@ind-cr.uclm.es>,
"Caml list" <caml-list@pauillac.inria.fr>
Subject: Re: Map is not tail recursive
Date: Mon, 11 Jan 1999 11:51:25 -0700 [thread overview]
Message-ID: <001101be3d93$66516610$210148bf@dylan> (raw)
Juan got me thinking about this problem... So here is a solution:
external rplacd : 'a list -> 'a list -> unit = "rplacd"
let list_map fn lst =
(* A properly tail recursive definition of Map *)
match lst with
[] -> [] (* OCAML represents [] specially - can't rplacd *)
| h :: t ->
let rslt = [fn h] in
let rec iter lst tail =
match lst with
[] -> rslt
| h :: t ->
let elt = [fn h] in
rplacd tail elt;
iter t elt
in
iter t rslt
-- and the external C code is
value rplacd(value cell, value item)
{
Store_field(cell,1,item);
return Val_unit;
}
-----Original Message-----
From: Juan Jose Garcia Ripoll <jjgarcia@ind-cr.uclm.es>
To: Caml list <caml-list@pauillac.inria.fr>
Date: Monday, January 11, 1999 02:26
Subject: Map is not tail recursive
>Hi,
>
>I've had a look at the List package and it seems that it is not properly
>tail recursive. Even more, for medium to large lists it exhausts the
>stack. I would suggest either recoding it as
>
>let map f a =
> let domap f done todo =
> match todo with
> [] -> List.reverse done
> | (x::xs) -> domap (f x)::done xs
> in domap f [] a
>
>Another possibility would be to introduce destructive operations such as
>Scheme's setcdr! and setcar!. This would eliminate the need of using
>List.reverse, at the cost of introducing some imperative style.
>
>Please excuse any mistake -- I'm quite new to this language.
>
>Regards
>
> Juanjo
>
next reply other threads:[~1999-01-12 9:45 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-01-11 18:51 David McClain [this message]
1999-01-13 8:47 ` Jacques GARRIGUE
-- strict thread matches above, loose matches on Subject: below --
1999-01-14 3:49 David McClain
1999-01-13 17:40 Marc Rouaix
1999-01-12 12:06 Marc Rouaix
1999-01-10 10:49 Juan Jose Garcia Ripoll
1999-01-11 11:03 ` Xavier Leroy
1999-01-12 11:49 ` William Chesters
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='001101be3d93$66516610$210148bf@dylan' \
--to=dmcclain@azstarnet.com \
--cc=caml-list@pauillac.inria.fr \
--cc=jjgarcia@ind-cr.uclm.es \
/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).