Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6863001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:43:46+00:00 2026-05-27T02:43:46+00:00

This recursive definition of a macro does what it should (sum integers from 1

  • 0

This recursive definition of a macro does what it should (sum integers from 1 to n):

(defmacro sum-int-seq (n)
  `(cond
     ((equal 0 ,n) 0)
     (t (+ ,n (sum-int-seq (- ,n 1))))))

For example (sum-int-seq 5) gives 15.

But why does it work? When the macro gets expanded i get this:

(macroexpand '(sum-int-seq 5))
(IF (EQUAL 0 5) 0 (+ 5 (SUM-INT-SEQ (- 5 1))))

But because sum-int-seq is a macro the macro evaluation should become an infinite loop. Does the compiler create a recursive function instead? If this definition creates a recursive function is there any way to define macros recursively?

(This is a silly example for the sake of brevity, a function would of course work better for this)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:43:47+00:00Added an answer on May 27, 2026 at 2:43 am

    Your example does not work.

    It may work in an interpreter. But with a compiler you’ll see an endless loop during compilation.

    CL-USER 23 > (defun test (foo)
                    (sum-int-seq 5))
    TEST
    

    Let’s use the LispWorks interpreter:

    CL-USER 24 > (test :foo)
    15
    

    Let’s try to compile the function:

    CL-USER 25 > (compile 'test)
    
    Stack overflow (stack size 15997).
      1 (continue) Extend stack by 50%.
      2 Extend stack by 300%.
      3 (abort) Return to level 0.
      4 Return to top loop level 0.
    
    Type :b for backtrace or :c <option number> to proceed.
    Type :bug-form "<subject>" for a bug report template or :? for other options.
    

    So, now the next question: why does it work in the interpreter, but the compiler can’t compile it?

    Okay, I’ll explain it.

    Let’s look at the interpreter first.

    • it sees (sum-int-seq 5).
    • it macroexpands it to (COND ((EQUAL 0 5) 0) (T (+ 5 (SUM-INT-SEQ (- 5 1))))).
    • it then evaluates above form. It determines that it needs to compute (+ 5 (SUM-INT-SEQ (- 5 1))). For that it needs to macroexpand (SUM-INT-SEQ (- 5 1)).
    • eventually it will expand into something like (cond ((EQUAL 0 (- (- (- (- (- 5 1) 1) 1) 1) 1)) 0) .... Which then will return 0 and the computation can use this result and add the other terms to it.

    The interpreter takes the code, evaluates what it can and macroexpands if necessary. The generated code is then evaluated or macroexpanded. And so on.

    Now let’s look at the compiler.

    • it sees (sum-int-seq 5) and macroexpands it into (COND ((EQUAL 0 5) 0) (T (+ 5 (SUM-INT-SEQ (- 5 1))))).
    • now the macroexpansion will be done on the subforms, eventually.
    • the compiler will macroexpand (SUM-INT-SEQ (- 5 1)). note that the code never gets evaluated, only expanded.
    • the compiler will macroexpand (SUM-INT-SEQ (- (- 5 1) 1)) and so forth. finally you’ll see a stack overflow.

    The compiler walks (recursively compiles / expands) the code. It may not execute the code (unless it does optimizations or a macro actually evaluates it explicitly).

    For a recursive macro you’ll need to actually count down. If you eval inside the macro, then something like (sum-int-seq 5) can made work. But for (defun foo (n) (sum-int-seq n)) this is hopeless, since the compiler does not know what the value of n is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often use this recursive 'visitor' in F# let rec visitor dir filter= seq
i have this recursive code <ul> <% sql=SELECT * FROM cats ORDER BY orderid
This is the definition of decidable from Wikipedia In computability theory, an undecidable problem
i am reading section about A Binary Recursive Gcd Algorithm,where this definition is given
I have this recursive method which deletes empty folders: private void DeleteEmpty(DirectoryInfo directory) {
This code involves a recursive Stored Procedure call and a not so great method
I read this answer on SO: Because the recursive mutex has a sense of
I wrote this snippet of code and I assume len is tail-recursive, but a
Imagine I have a recursive algebraic data type like this (Haskell syntax): data Expr
Why is the recurrence relation of recursive factorial algorithm this? T(n)=1 for n=0 T(n)=1+T(n-1)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.