From: Martin Jambon <martin.jambon@ens-lyon.org>
To: Martin Jambon <martin.jambon@ens-lyon.org>
Cc: "Stéphane Glondu" <steph@glondu.net>,
"Warren Harris" <warren@metaweb.com>, OCaml <caml-list@inria.fr>
Subject: Re: [Caml-list] lazy vs fun
Date: Tue, 25 Aug 2009 01:11:55 +0200 [thread overview]
Message-ID: <4A931E3B.4010501@ens-lyon.org> (raw)
In-Reply-To: <4A9311E4.7060600@ens-lyon.org>
Martin Jambon wrote:
> Stéphane Glondu wrote:
>> Warren Harris a écrit :
>>> Is there any advantage to using lazy evaluation in ocaml rather than
>>> just using thunks to defer evaluation? [...]
>> Two things I can think of right now: they are evaluated only once (even
>> if you call Lazy.force several times), and you can do pattern matching
>> with them.
>
>
> Note that the memoization feature can be implemented like this:
>
> let lz f =
> let result = ref `None in
> fun () ->
> match !result with
> `None ->
> (try
> let y = f () in
> result := `Result y;
> y
> with e ->
> result := `Exn e;
> raise e
> )
> | `Result y -> y
> | `Exn e -> raise e
Oops.
The following makes it possible for f to be garbage-collected:
let lz f =
let result = ref (`None f) in
fun () ->
match !result with
`None f ->
(try
let y = f () in
result := `Result y;
y
with e ->
result := `Exn e;
raise e
)
| `Result y -> y
| `Exn e -> raise e
Martin
--
http://mjambon.com/
next prev parent reply other threads:[~2009-08-24 23:18 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-08-24 21:57 Warren Harris
2009-08-24 22:04 ` [Caml-list] " Jake Donham
2009-08-24 22:15 ` Warren Harris
2009-08-24 22:18 ` Jake Donham
2009-08-24 22:06 ` Stéphane Glondu
2009-08-24 22:19 ` Martin Jambon
2009-08-24 23:11 ` Martin Jambon [this message]
2009-08-24 23:33 ` Warren Harris
2009-08-25 5:19 ` rixed
2009-08-25 6:29 ` David Allsopp
2009-08-25 18:09 ` rixed
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=4A931E3B.4010501@ens-lyon.org \
--to=martin.jambon@ens-lyon.org \
--cc=caml-list@inria.fr \
--cc=steph@glondu.net \
--cc=warren@metaweb.com \
/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