
Handling yielded values
(defmacro with-coroutine (coroutine value &body body)
`(restart-case
(handler-bind ((yield (lambda (condition)
(let ((,value (value condition)))
,@body))))
,coroutine)
(abort ())))
(defun ssq (n)
(let ((step 0)
(sum 0))
(with-coroutine (squares) sq
(incf sum sq)
(incf step)
(when (> step n)
(abort)))
sum))
(defun leaves (tree)
(let (leaves)
(with-coroutine (preorder tree) leaf
(push leaf leaves))
(nreverse leaves)))