递归

递归

July 24, 2022
plt

递归 #

(define (sum n)
  (if (zero? n)
      0
      (+ n (sum (- n 1))))) ; not tail recursive

尾递归 #

(define (tail-sum n [acc 0])
  (if (zero? n)
      acc
      (tail-sum (- n 1) (+ n acc)))) ; tail recursive