Michele Simionato <michele.simionato@[EMAIL PROTECTED]
> wrote:
> I see that both SML/NJ and MLTon provide continuations, however their
> usage is somewhat different than in Scheme.
Well, a difference is that callcc p***** the continuation as a value of an
abstract type, rather than as a function, to the given function. Then you
use throw instead of function application to jump to a continuation. One
reason for this arrangement is allowing throw to be called from a context
of any type. See the following article:
Typing First-Class Continuations in ML
Robert Harper, Bruce F. Duba, and David MacQueen
http://citeseer.comp.nus.edu.sg/11210.html
> Can somebody provide a translation of the following toy Scheme program
> into SML?
> (define-macro (store/cc! name . body)
> (let ((k (gensym)))
> `(call-with-current-continuation
> (lambda (,k) (set! ,name ,k) ,@[EMAIL PROTECTED]
))))
open SMLofNJ.Cont
fun storecc r th = callcc (fn k => (r := SOME k ; th ()))
> (define cont #f); make room for the continuation
val cont : int cont option ref = ref NONE
> (display (+ 1 (store/cc! cont 0))) ;=> displays 1
> (cont 1) ;=> displays 2
The above two lines form a loop, although, if you run the above from
Bigloo's REPL, the continuation captured on the display expression seems
to be limited to that expression. Running the above directly, as two
top-level expressions,
print (Int.toString (1 + storecc cont (fn () => 0))) ;
throw (valOf (!cont)) 1 ;
in SML/NJ's REPL gives
Error: throw from one top-level expression into another
So, in SML/NJ's REPL they need to be merged into a single top-level
expression and make sure it doesn't loop:
val () =
(print (Int.toString (1 + storecc cont (fn () => 0)))
; case !cont
of SOME k => (cont := NONE ; throw k 1)
| NONE => ())
-Vesa Karvonen


|