From: Andrej Bauer <Andrej.Bauer@fmf.uni-lj.si>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Which control structure?
Date: Wed, 03 Oct 2007 00:24:13 +0200 [thread overview]
Message-ID: <4702C50D.6040606@fmf.uni-lj.si> (raw)
In-Reply-To: <470290BC.8020604@fmf.uni-lj.si>
If anybody cares, I can do what I want in SML/NJ using a particularly
ugly combination of callcc and store, see below. Is there a sane way to
achieve the same thing? I suspect I want delimited continuations, but I
am not sure.
-----
open SMLofNJ.Cont ;
(* A perverse way of computing (p false, p true) by invoking p only once. *)
fun two p =
let val c = ref NONE (* here we store the continuation *)
val a = ref NONE (* here we store p false *)
val b = ref NONE (* here we store p true *)
in
(* store p true into a, and save the continuation *)
a := SOME (p (callcc (fn k => (c := SOME k; true)))) ;
(* see if we're hapenning the first or the second time *)
if !b = NONE then (
(* first time around *)
b := !a ; (* store p false into b *)
(* reinvoke to compute p true *)
let val SOME k = !c in throw k false end)
else
(* second time around *)
let val SOME x = !a
val SOME y = !b
in
(x, y)
end
end
-----
Andrej
next prev parent reply other threads:[~2007-10-02 22:24 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-10-02 18:41 Andrej Bauer
2007-10-02 22:24 ` Andrej Bauer [this message]
2007-10-03 8:00 [Caml-list] " oleg
2007-10-03 9:30 ` Andrej Bauer
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=4702C50D.6040606@fmf.uni-lj.si \
--to=andrej.bauer@fmf.uni-lj.si \
--cc=Andrej.Bauer@andrej.com \
--cc=caml-list@yquem.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