From: Jake Donham <jake@donham.org>
To: OCaml <caml-list@inria.fr>
Subject: Re: [Caml-list] lazy vs fun
Date: Mon, 24 Aug 2009 15:04:36 -0700 [thread overview]
Message-ID: <c7e4e9f0908241504m51d796b2wa49f0b0df84f603d@mail.gmail.com> (raw)
In-Reply-To: <94AD5806-B6F6-44F7-AA3C-1E63B6C1A722@metaweb.com>
On Mon, Aug 24, 2009 at 2:57 PM, Warren Harris<warren@metaweb.com> wrote:
> Is there any advantage to using lazy evaluation in ocaml rather than just
> using thunks to defer evaluation? E.g.
>
> let x = lazy (3+4)
> let y = Lazy.force x
>
> vs:
>
> let x = fun () -> 3+4
> let y = x ()
Lazy cells don't just defer, they also memoize the returned value once
the cell is forced.
# let x = lazy (print_endline "forced"; 1);;
val x : int lazy_t = <lazy>
# Lazy.force x;;
forced
- : int = 1
# Lazy.force x;;
- : int = 1
They even memoize exceptions:
# let x = lazy (print_endline "forced"; failwith "failed");;
val x : 'a lazy_t = <lazy>
# Lazy.force x;;
forced
Exception: Failure "failed".
# Lazy.force x;;
Exception: Failure "failed".
Jake
next prev parent reply other threads:[~2009-08-24 22:04 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 ` Jake Donham [this message]
2009-08-24 22:15 ` [Caml-list] " 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
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=c7e4e9f0908241504m51d796b2wa49f0b0df84f603d@mail.gmail.com \
--to=jake@donham.org \
--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