Version française
Home     About     Download     Resources     Contact us    
Browse thread
Which control structure?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Andrej Bauer <Andrej.Bauer@f...>
Subject: Re: [Caml-list] Which control structure?
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