TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Q about Exercise 10.4 in "ANSI Common Lisp"

7 pointsby paul_reinersabout 17 years ago
I came up with the following solution for Exercise 4 in Chapter 10 of "ANSI Common Lisp" by Paul Graham:<p><pre><code> (defmacro ntimes (n &#38;rest body) (let ((h (gensym))) `(let ((,h ,n)) (if (&#62; ,h 0) ,@body) (if (zerop ,h) nil (ntimes (1- ,h) ,@body))))) </code></pre> 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:<p><pre><code> (ntimes 10 (princ ".")) </code></pre> I get the following error:<p>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.<p>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?

2 comments

pgabout 17 years ago
You've written a recursive macro. That is usually a mistake. Here (unless you mean the first argument always to be a constant) you're recursing based on values that won't be available till runtime, yet the macro could be expanded at compile-time.<p>You should just expand into a do.
评论 #143487 未加载
paul_reinersabout 17 years ago
Sorry about the non-pretty printing. It was nicely formatted, when I entered it (I swear). Is there something like a &#60;code&#62; tag I could use?<p>Also, not all of the error message printed. What's up with that?