Saturday, September 23, 2006

arnesi continuations

I've just moved back to the UK from Australia. With a few days of jet-lagged downtime I've managed to fit in some fiddling with CL. The CVS version of SBCL now appears to work with Slime under Win32 (which is very very cool), and I've been playing with with-call/cc from arnesi. It took a bit of fiddling to realise that you need to use defun/cc - documentation seems to be quite thin on the ground. Once I twigged, it was pretty easy to port Fig 22.4 of OnLisp to CL:

(defparameter *paths* nil)

(defun/cc choose (choices)
(if (null choices)
(let/cc cc
(push (lambda () (kall cc (choose (cdr choices)))) *paths*)
(kall cc (car choices)))))

(defun/cc fail ()
(if (null *paths*)
(kall arnesi::*toplevel-k* '@)
(funcall (pop *paths*))))

So that now Fig 22.3 can be written:

(defun/cc two-numbers ()
(list (choose '(0 1 2 3 4 5))
(choose '(0 1 2 3 4 5))))

(defun parlor-trick (sum)
(let ((nums (two-numbers)))
(if (= (apply #'+ nums) sum)
`(the sum of ,@nums)