Public feed #888180

ehird

public text v1 · immutable
Raw Edit
·published 2008-02-01 22:41 UTC
rendered paste body
;; regular
(define (iter-rec i t b s c)
  (if (t i)
      b
      (c i (iter-rec (s i) t b s c))))

;; tail recursive
(define (iter-rec i t b s c)
  (let loop ((v i) (a b))
    (if (t v)
        a
        (loop (s v) (c v a)))))

;; factorial
(define (fact n)
  (iter-rec n zero? 1 sub1 *))

;; plugged into regular (literal! try it yourself)
(define (fact n)
  (if (zero? n)
      1
      (* n (fact (sub1 n)))))

;; plugged into tail recursive
(define (fact n)
  (let loop ((v n) (a 1))
    (if (zero? v)
        a
        (loop (sub1 v) (* v a)))))