Saturday, January 21, 2006

Using let outside functions

I was writing defvars for functions that needed to share state information.

(defvar *splitlevel* 0)

(defun foo ()
(do something with *splitlevel*))

(defun bar ()
(do something else with *splitlevel*))

I've now found the use of let to wrap up several functions at once:

(let ((splitlevel 0))
(defun foo ()
(do something with splitlevel))

(defun bar ()
(do something else with splitlevel)))

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.

Monday, January 16, 2006

Piling On

I just love an underdog, what can I say? Give me a new technology outside of the mainstream, and like some geeky magpie, I just can't resist the temptation of a shiney new toy. Linux, BeOS, OS X... I've had love affairs with them all. It turns out my latest love affair is not so new and shiney, but like the NBC tagline: "If you haven't seen it, It's new to YOU."

Yep, I'm really getting into Lisp.