Lisp Recursion Examples, Tail Recursion
Using Cond with Recursion
(if (= exp 0)
1
(* base (my-exp base (- exp 1)))))
2. With tail recursion:
(defun my-exp (base exp)
(my-exp-aux base exp 1))
(defun my-exp-aux (base exp product)
(if (= exp 0)
power
(my-exp-aux base (- exp 1) (* base product))))
3. (defun fibonacci (n)
(if (or (= n 0) (= n 1))
1
(+ (fibonacci (- n 1))
(fibonacci (- n 2)))))
With cond:
(defun fibonacci (n)
(cond ((= n 0) 1)
((= n 1) 1)
(t (+ (fibonacci (- n 1))
(fibonacci (- n 2))))))