Skip to content

Instantly share code, notes, and snippets.

@keyvanakbary
Created January 3, 2016 16:09
Show Gist options
  • Save keyvanakbary/ea825d52027aabeb1088 to your computer and use it in GitHub Desktop.
Save keyvanakbary/ea825d52027aabeb1088 to your computer and use it in GitHub Desktop.
; LINEAR RECURSION
(define (factorial n)
(if (= n 1)
1
(* n (factorial (- n 1)))))
(factorial 3)
;(* 3 (factorial 2))
;(* 3 (* 2 (factorial 1)))
;(* 3 (* 2 1))
;(* 3 2)
;6
; LINEAR ITERATION
(define (factorial n)
(fact-iter 1 1 n))
(define (fact-iter product counter max-count)
(if (> counter max-count)
product
(fact-iter (* counter product)
(+ counter 1)
max-count)))
(factorial 3)
;(fact-iter 1 1 3)
;(fact-iter 1 2 3)
;(fact-iter 2 3 3)
;(fact-iter 6 4 3)
;6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment