Skip to content

charJe/tailrec

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tailrec

A simple macro to optimize a function for tail recursion.

Install

Use quicklisp.

(ql:quickload 'tailrec)

Use

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

Issues

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.

Contact

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.

About

A simple macro to optimize a function for tail recursion. THIS PROJECT HAS MOVED

Resources

License

Stars

Watchers

Forks