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

  • SEARCH
  • Home
  • 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 7809149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:20:08+00:00 2026-06-02T03:20:08+00:00

Is it possible to write a Common Lisp macro that takes a list of

  • 0

Is it possible to write a Common Lisp macro that takes a list of dimensions and variables, a body (of iteration), and creates the code consisting of as many nested loops as specified by the list?

That is, something like:

(nested-loops '(2 5 3) '(i j k) whatever_loop_body)

should be expanded to

(loop for i from 0 below 2 do
  (loop for j from 0 below 5 do
    (loop for k from 0 below 3 do
      whatever_loop_body)))

Follow up

As huaiyuan correctly pointed out, I have to know the parameters to pass to macro at compile time. If you actually need a function as I do, look below.

If you are ok with a macro, go for the recursive solution of 6502, is wonderful.

  • 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-06-02T03:20:09+00:00Added an answer on June 2, 2026 at 3:20 am

    You don’t need the quotes, since the dimensions and variables need to be known at compile time anyway.

    (defmacro nested-loops (dimensions variables &body body)
      (loop for range in (reverse dimensions)
            for index in (reverse variables)
            for x = body then (list y)
            for y = `(loop for ,index from 0 to ,range do ,@x)
            finally (return y)))
    

    Edit:

    If the dimensions cannot be decided at compile time, we’ll need a function

    (defun nested-map (fn dimensions)
      (labels ((gn (args dimensions)
                 (if dimensions
                   (loop for i from 0 to (car dimensions) do
                     (gn (cons i args) (cdr dimensions)))
                   (apply fn (reverse args)))))
        (gn nil dimensions)))
    

    and to wrap the body in lambda when calling.

    CL-USER> (nested-map (lambda (&rest indexes) (print indexes)) '(2 3 4))
    
    (0 0 0) 
    (0 0 1) 
    (0 0 2) 
    (0 0 3) 
    (0 0 4) 
    (0 1 0) 
    (0 1 1) 
    (0 1 2) 
    (0 1 3) 
    (0 1 4) 
    (0 2 0) 
    (0 2 1) 
    ...
    

    Edit(2012-04-16):

    The above version of nested-map was written to more closely reflect the original problem statement. As mmj said in the comments, it’s probably more natural to make index range from 0 to n-1, and moving the reversing out of the inner loop should improve efficiency if we don’t insist on row-major order of iterations. Also, it’s probably more sensible to have the input function accept a tuple instead of individual indices, to be rank independent. Here is a new version with the stated changes:

    (defun nested-map (fn dimensions)
      (labels ((gn (args dimensions)
                 (if dimensions
                   (loop for i below (car dimensions) do
                     (gn (cons i args) (cdr dimensions)))
                   (funcall fn args))))
        (gn nil (reverse dimensions))))
    

    Then,

    CL-USER> (nested-map #'print '(2 3 4))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to How to Write Tests That Share Common Set-Up Code is it possible
Is it possible to write a program that will change the phone numbers a
Is it possible to write a script in Perl that opens different URLs and
Would it be possible to write a script that gave the user the ability
Is it possible to write the folowing using lambda(C#) private static void GetRecordList(List<CustomerInfo> lstCustinfo)
With C++03 it was (and still is) possible to write cross-platform code with both
In Common Lisp I can conditionally exclude or include code for different implementations like
In basic Java, if I write code to solve a common problem and want
I'm trying to write a function in Common Lisp similar to the built in
First, I am very new to lisp, so it is possible that I'm just

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.