Tail Call Optimization in Elisp

If you’ve got a Lisp background—especially a Scheme background—you may have internalized recursion as a looping mechanism and general programming strategy. That doesn’t work with Elisp, of course, because each recursive invocation allocates a stack frame. Not to worry, though, Wilfred Hughes has got you covered.

His tco.el provides a macro and some trampoline code that allow you to define a function that can be called tail recursively without worrying about exhausting memory with stack frame allocations. This isn’t something you’ll want to do that often in Elisp but there’s been a couple times when the natural solution to an Emacs project I was working on called for tail recursion and I missed having it. Even if you don’t worship at the church of recursion, you’ll have to admit that it’s a nice hack.

This entry was posted in Programming and tagged , . Bookmark the permalink.