I came up with the following solution for Exercise 4 in Chapter 10 of "ANSI Common Lisp" by Paul Graham:
(defmacro ntimes (n &rest body)
(let ((h (gensym)))
`(let ((,h ,n))
(if (> ,h 0)
,@body)
(if (zerop ,h)
nil
(ntimes (1- ,h) ,@body)))))
Using GNU CLISP 2.44, this works fine. However, when I use SBCL 1.0.6 via Cusp v0.8.174 and do the following call:
(ntimes 10
(princ "."))
I get the following error:
Control stack exhausted (no more space for function call frames). This is probably due to heavily nested or infinitely recursive function calls, or a tail call that SBCL cannot or has not optimized away.
What exactly is going on here? Is my solution correct and SBCL is wrong, or is my solution incorrect but it just happens to work on CLISP?
You should just expand into a do.