no continuations

Kevin S. Millikin kmillikin at atcorp.com
Tue Dec 30 13:18:13 EST 2003


On Tuesday, December 30, 2003 12:39 PM, Ben Rudiak-Gould 
[SMTP:benrg at dark.darkweb.com] wrote:
>
> With letrec and unrestricted call/cc you can implement ML-style refs:

With an *implementation of letrec that uses mutation* and unrestricted 
call/cc, you can implement ML-style ref cells:

Petite Chez Scheme Version 6.0a
Copyright (c) 1998 Cadence Research Systems

> (define-syntax letrec^
    (syntax-rules ()
      ((_ ((Var Exp)) Body Bodies ...)
       (let ((Y (lambda (f)
                  ((lambda (g) (f (g g)))
                   (lambda (g) (f (lambda (x) ((g g) x))))))))
         (let ((Var (Y (lambda (Var) Exp))))
           Body Bodies ...)))))

> (letrec^ ((fact (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))))
    (fact 5))
120

> (define (make-cell)
    (call/cc
     (lambda (return-from-make-cell)
       (letrec^ ((state
                   (call/cc
                     (lambda (return-new-state)
                       (return-from-make-cell
                         (lambda (op)
                           (case op
                             ((set)
                              (lambda (value)
                                (call/cc
                                  (lambda (return-from-access)
                                    (return-new-state
                                      (list value 
return-from-access))))))
                             ((get) (car state)))))))))
         ((cadr state) 'done)))))

> (define c (make-cell))

> ((c 'set) 3)

> (c 'get)
3

> ((c 'set) 7)
done

> (c 'get)
3


More information about the Haskell mailing list