Skip to content

Instantly share code, notes, and snippets.

@hanachin
Created November 18, 2010 15:17
Show Gist options
  • Save hanachin/705104 to your computer and use it in GitHub Desktop.
Save hanachin/705104 to your computer and use it in GitHub Desktop.
calculate factorial of 5 in 4 ways
(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