From 4aa0e0d6756614ee1574f249be1369cc45e5a88d Mon Sep 17 00:00:00 2001 From: Fernando Basso Date: Thu, 30 Nov 2017 08:06:24 -0200 Subject: [PATCH] dq03: Refactor factorial and make it tail recursive. Use of result so far accumulator. --- 10-accumulators/dq03-factorial-tr.rkt | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 10-accumulators/dq03-factorial-tr.rkt diff --git a/10-accumulators/dq03-factorial-tr.rkt b/10-accumulators/dq03-factorial-tr.rkt new file mode 100644 index 0000000..15fdc5c --- /dev/null +++ b/10-accumulators/dq03-factorial-tr.rkt @@ -0,0 +1,36 @@ +#lang htdp/isl + +; PROBLEM 3: +; +; Refactor the function below to make it tail recursive. +; + +;; Natural -> Natural +;; produces the factorial of the given number +;(check-expect (fact 0) 1) +;(check-expect (fact 3) 6) +;(check-expect (fact 5) 120) +#; +(define (fact n) + (cond [(zero? n) 1] + [else + (* n (fact (sub1 n)))])) + +(define (fact n0) + ;; acc is Natural; the computation of the factorial so far. + ;; acc is a "result so far" type of accumulator. + ;; (fact 5 1) ; outer call + ;; (fact 5 1) ; 5 + ;; (fact 4 5) ; 20 + ;; (fact 3 20) ; 60 + ;; (fact 2 60) ; 120 + ;; (fact 1 120) ; 120 + ;; (fact 0 120) ; 120 + (local [(define (fact n acc) + ;; This takes one more recursion step. + ;(cond [(zero? n) acc] + (cond [(= n 1) acc] + [else + (fact (sub1 n) (* n acc))]))] + (fact n0 1))) +