A simple macro to optimize a function for tail recursion.
Use quicklisp.
(ql:quickload 'tailrec)
Just wrap your function definition with (tailrec )
:
(tailrec
(defun fib (n &optional (a 0) (b 1))
"Return the Nth Fibonnaci number."
(cond
((= 0 n) a)
((= 1 n) b)
(:else (fib (- n 1) b (+ a b))))))
If your function is not tail recursive, tailrec
will give warnings and not optimize.
Also provided is the nlet
macro which is similar to Clojure’s loop
. It automatically uses tailrec
.
(nlet fib ((n 10)
(a 0)
(b 1))
(cond
((= 0 n) a)
((= 1 n) b)
(:else (fib (- n 1) b (+ a b)))))
;=> 55
Common Lisp is a very complex beast, so I probably am missing something here. It will work for some things, but might not work for others. If you have any issues use the Github issue tracker.
If you are not having issues, but still want to contact me for collaboration, licensing, or something else, you can use the email in tailrec.asd
.