Saturday, March 24, 2012

Recur for Emacs Lisp now Stand Alone

Hi blog readers. In the past I've been very interested in extending emacs lisp with modern functional programming idioms. One of the features I like from clojure is self-recursion optimization, which I ported to emacs lisp awhile ago. It lets you write code like:
    (recur-defun* my-prod (list &optional (acc 1))
      (if list (recur (cdr list) (* acc (car list)))
          acc))
    (my-prod (list 1 2 3 4)) 

My previous code depended on my giant mess of utility library and didn't have a separate git repository. This made it hard to use and maintain. To enable more ease of use, I've factored out all the dependencies (except for cl.el) and created a separate repo on github. All you need to do to use it now is place it on your load path and say (require 'recur). One day I'll factor out the `cl.el` requirement, since apparently depending on that library is frowned upon by emacs.

3 comments:

Tavis Rudd (openid only - no blog) said...

Awesome. I've wanted to use many of your little elisp goodies for some time but couldn't because of that mess of dependencies and the high chance of name clobbering.

Tavis
p.s. cl.el isn't frowned upon by everyone http://tromey.com/blog/?p=751 ;)

J.V. Toups said...

Tavis,

Glad you like it. You would probably also be interested in a new library I've written, which is also stand alone, which subsumes many of my older elisp extension projects.

Shadchen-el provides extensible racket-like pattern matching in emacs lisp, which is a much more powerful version of the simple destructuring bind in Clojure.

I just added a `match-let` form which is like a let expression but allows full patterns in the variable slots. Within `match-let` `recur` in a tail position triggers a trampoline re-entry into the the match expression.

It doesn't check that recur only occurs in tail position like recur.el, but I will add that feature soon.

Tavis Rudd (openid only - no blog) said...

Very nice!