| 1 |
;;; FIBC -- FIB using first-class continuations, written by Kent Dybvig |
| 2 |
|
| 3 |
(define (_1+ n) (+ n 1)) |
| 4 |
(define (_1- n) (- n 1)) |
| 5 |
|
| 6 |
;;; fib with peano arithmetic (using numbers) with call/cc |
| 7 |
|
| 8 |
(define (addc x y k) |
| 9 |
(if (zero? y) |
| 10 |
(k x) |
| 11 |
(addc (_1+ x) (_1- y) k))) |
| 12 |
|
| 13 |
(define (fibc x c) |
| 14 |
(if (zero? x) |
| 15 |
(c 0) |
| 16 |
(if (zero? (_1- x)) |
| 17 |
(c 1) |
| 18 |
(addc (call-with-current-continuation (lambda (c) (fibc (_1- x) c))) |
| 19 |
(call-with-current-continuation (lambda (c) (fibc (_1- (_1- x)) c))) |
| 20 |
c)))) |
| 21 |
|
| 22 |
(define (main) |
| 23 |
(run-benchmark |
| 24 |
"fibc" |
| 25 |
fibc-iters |
| 26 |
(lambda (result) (equal? result 2584)) |
| 27 |
(lambda (x c) (lambda () (fibc x c))) |
| 28 |
18 |
| 29 |
(lambda (n) n))) |