Created
January 3, 2016 16:09
-
-
Save keyvanakbary/ea825d52027aabeb1088 to your computer and use it in GitHub Desktop.
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
; 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