Skip to content

Instantly share code, notes, and snippets.

@aj07mm
Created September 2, 2020 20:43
Show Gist options
  • Save aj07mm/bfc02b65b4b2395da437053a4a86484d to your computer and use it in GitHub Desktop.
Save aj07mm/bfc02b65b4b2395da437053a4a86484d to your computer and use it in GitHub Desktop.
tail-rect.scm
(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