(define (return x) (tag 'done x))
(define (bounce thunk) (tag 'doing thunk))
(define (call thunk) (thunk))
(define (tag label thing) (cons label thing))
(define tag-of car)
(define tag-value cdr)
(define (pogo-stick thread)
(cond ((eqv? 'done (tag-of thread))
(tag-value thread))
((eqv? 'doing (tag-of thread))
(pogo-stick (call (tag-value thread))))))
(define (fact-iter n acc)
(if (zero? n)
(return acc)
(bounce
(lambda ()
(fact-iter
(- n 1)
(* acc n))))))
(define (mem? n lst)
(cond ((null? lst) (return #f))
((= (car lst ) n) (return #t))
(else (bounce
(lambda ()
(mem? n (cdr lst)))))))
(define (fib n)
(fib-iter n 0 0 1))
(define (fib-iter n i small large)
(if (< i n)
(bounce
(lambda ()
(fib-iter n (+ i 1) large (+ large small))))
(return small)))
; > (pogo-stick (fact-iter 5 1))
; 120
; > (pogo-stick (mem? 5 (list 1 2 3 4 5 6)))
; #t
; > (pogo-stick (fib 8))
; 21 | |
Stepwise execute the thread until a done value is encountered.
The value of a 'done thread' is returned.
The value of 'doing thread' is evaluated, and the calculation continues.
|