Last active
February 19, 2018 12:33
-
-
Save Yoxem/f2401de600477b89d3d018737d2920f9 to your computer and use it in GitHub Desktop.
A demo of continuation-pass-style (CPS) by finding the n-th number of fibonacci series with R5RS Scheme.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; a demo of continuation-pass-style by finding the n-th number of fibonacci series. | |
;; works in R5RS scheme | |
(define +& | |
(lambda (a b y) | |
(y (+ a b) | |
))) | |
(define <& | |
(lambda (a b y) | |
(y (< a b) | |
))) | |
(define =& | |
(lambda (a b y) | |
(y (= a b) | |
))) | |
(define (fib& n y) | |
(<& n 2 (lambda (nlt2) | |
(if nlt2 | |
(y 1) | |
(fib-iter& n 2 1 1 y) | |
)))) | |
(define (fib-iter& n i fib_n-1 fib_n-2 y) | |
(+& fib_n-1 fib_n-2 (lambda (fib-n) | |
(let | |
((fib_n fib-n)) | |
(=& n i (lambda (neqi) | |
(if neqi | |
(y fib_n) | |
(+& i 1 (lambda (i+1) | |
(fib-iter& n i+1 fib_n fib_n-1 y) | |
))))))))) | |
(fib& 1 (lambda(x) x)) | |
(fib& 10 (lambda(x) x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment