Friday, January 20, 2006

&optional labels

I'm trying to document different lisp coding conventions as I run across them. For instance, I was writing recursive functions like:

(defun foo (x &optional (y 0))
(progn ...
(foo x (1+ y))))

This just felt wrong - very wrong. I'm trying to make it more convenient for the caller (me in some future coding life, like say 60 minutes later) that doesn't care about properly initializing the accumulator or index. If since come across the following form, which just feels more comfortable to me:

(defun foo (x)
(labels ((find-foo (x y)
....
(foo x (1+ y)) ... )
(find-foo x 0)))

It is more verbose, I know, but somehow that (defun foo (x) ... ) just seems so much cleaner. This is of course predicated on the notion that you will NEVER want to call foo with an intermediate y value directly.

0 Comments:

Post a Comment

<< Home