Created
November 18, 2010 15:17
-
-
Save hanachin/705104 to your computer and use it in GitHub Desktop.
calculate factorial of 5 in 4 ways
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
(define (fact n) | |
(if (= 0 n) | |
1 | |
(* n (fact (- n 1))))) | |
(fact 5) | |
(define (fact n) | |
(define (iter n result) | |
(if (= 0 n) | |
result | |
(iter (- n 1) (* n result)))) | |
(iter n 1)) | |
(fact 5) | |
(define (fact n) | |
(define (fact/cps n cont) | |
(if (= 0 n) | |
(cont 1) | |
(fact/cps (- n 1) | |
(lambda (x) | |
(cont (* n x)))))) | |
(fact/cps n (lambda (x) x))) | |
(fact 5) | |
(((lambda (f) | |
((lambda (proc) | |
(f (lambda (arg) ((proc proc) arg)))) | |
(lambda (proc) | |
(f (lambda (arg) ((proc proc) arg)))))) | |
(lambda (f) | |
(lambda (n) | |
(if (= 0 n) | |
1 | |
(* n (f (- n 1))))))) 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment