(repl) ;;; We don't really need DEFINE! ((lambda (f1) (f1 f1 20)) (lambda (f n) (cond ((< n 2) n) (else (+ (f f (- n 2)) (f f (- n 1))))))) ;==> 6765 ;;; Escaping continuations (+ 1 (call/cc (lambda (k) (+ 2 (k 3))))) ;==> 4 ((lambda (map) (call/cc (lambda (k) (map map (lambda (x) (cond ((negative? x) (k 'bad)) (else (sqrt x)))) '(1 9 25 64))))) (lambda (me proc lst) (cond ((null? lst) lst) (else (cons (proc (car lst)) (me me proc (cdr lst))))))) ;==> (1 3 5 8) ((lambda (map) (call/cc (lambda (k) (map map (lambda (x) (cond ((negative? x) (k 'bad)) (else (sqrt x)))) '(1 9 -25 64))))) (lambda (me proc lst) (cond ((null? lst) lst) (else (cons (proc (car lst)) (me me proc (cdr lst))))))) ;==> bad ;;; Let's now assume we have DEFINE, SET!, etc. (define map (lambda (proc lst) (cond ((null? lst) lst) (else (cons (proc (car lst)) (map proc (cdr lst))))))) (cons 'bar (call/cc (lambda (k) (map (lambda (x) (cond ((negative? x) (k 'bad)) (else (sqrt x)))) '(1 9 25 64))))) ;==> (foo 1 3 5 8) (cons 'bar (call/cc (lambda (k) (map (lambda (x) (cond ((negative? x) (k 'bad)) (else (sqrt x)))) '(1 9 -25 64))))) ;==> (foo . bad) ;;; A continuation has infinite extent, like ;;; any other procedure. (define r #f) (+ 1 (call/cc (lambda (k) (set! r k) (+ 2 (k 3))))) ;==> 4 (r 5) ;==> 6 ;;; Indeed! It abandons its own continuation. (+ 3 (r 5)) ;==> 6 ;;; We'll see lots more of this later!