Created
September 2, 2020 20:43
-
-
Save aj07mm/bfc02b65b4b2395da437053a4a86484d to your computer and use it in GitHub Desktop.
tail-rect.scm
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 x) | |
(if (= x 0) 1 | |
(* x (fact (- x 1))))) | |
(define (fact x) | |
(define (fact-tail x accum) | |
(if (= x 0) accum | |
(fact-tail (- x 1) (* x accum)))) | |
(fact-tail x 1)) | |
(fact 3) | |
;cont ;acc | |
(fact-tail (- 3 1) (* 3 1)) | |
2 3 | |
(fact-tail (- 2 1) (* 2 3)) | |
1 6 | |
(fact-tail (- 1 1) (* 1 6)) | |
0 6 | |
(fact-tail (- 0 1) (* 0 6)) | |
-1 base-case: return 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment