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 8380467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:27:58+00:00 2026-06-09T16:27:58+00:00

Can someone help me understand how push can be implemented as a macro? The

  • 0

Can someone help me understand how push can be implemented as a macro? The naive version below evaluates the place form twice, and does so before evaluating the element form:

(defmacro my-push (element place)
  `(setf ,place (cons ,element ,place)))

But if I try to fix this as below then I’m setf-ing the wrong place:

(defmacro my-push (element place)
   (let ((el-sym    (gensym))
         (place-sym (gensym)))
     `(let ((,el-sym    ,element)
            (,place-sym ,place))
        (setf ,place-sym (cons ,el-sym ,place-sym)))))

CL-USER> (defparameter *list* '(0 1 2 3))
*LIST*
CL-USER> (my-push 'hi *list*)
(HI 0 1 2 3)
CL-USER> *list*
(0 1 2 3)

How can I setf the correct place without evaluating twice?

  • 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-09T16:27:59+00:00Added an answer on June 9, 2026 at 4:27 pm

    Doing this right seems to be a little more complicated. For instance, the code for push in SBCL 1.0.58 is:

    (defmacro-mundanely push (obj place &environment env)
      #!+sb-doc
      "Takes an object and a location holding a list. Conses the object onto
      the list, returning the modified list. OBJ is evaluated before PLACE."
      (multiple-value-bind (dummies vals newval setter getter)
          (sb!xc:get-setf-expansion place env)
        (let ((g (gensym)))
          `(let* ((,g ,obj)
                  ,@(mapcar #'list dummies vals)
                  (,(car newval) (cons ,g ,getter))
                  ,@(cdr newval))
             ,setter))))
    

    So reading the documentation on get-setf-expansion seems to be useful.

    For the record, the generated code looks quite nice:

    Pushing into a symbol:

    (push 1 symbol)
    

    expands into

    (LET* ((#:G906 1) (#:NEW905 (CONS #:G906 SYMBOL)))
      (SETQ SYMBOL #:NEW905))
    

    Pushing into a SETF-able function (assuming symbol points to a list of lists):

    (push 1 (first symbol))
    

    expands into

    (LET* ((#:G909 1)
           (#:SYMBOL908 SYMBOL)
           (#:NEW907 (CONS #:G909 (FIRST #:SYMBOL908))))
      (SB-KERNEL:%RPLACA #:SYMBOL908 #:NEW907))
    

    So unless you take some time to study setf, setf expansions and company, this looks rather arcane (it may still look so even after studying them). The ‘Generalized Variables’ chapter in OnLisp may be useful too.

    Hint: if you compile your own SBCL (not that hard), pass the --fancy argument to make.sh. This way you’ll be able to quickly see the definitions of functions/macros inside SBCL (for instance, with M-. inside Emacs+SLIME). Obviously, don’t delete those sources (you can run clean.sh after install.sh, to save 90% of the space).

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

Sidebar

Related Questions

Can someone help me understand how to write this case statement properly its not
(Specifically, I'm using Backbone Model events) Can someone help me understand how javascript events
can someone help me understand this because it's working in as3 but not so
Can someone help me understand why this gives an output of 0? #include <iostream>
Can someone help me understand this topshelf project...mainly the difference between ConfigureServiceinIsolation and ConfigureService.
Eclipse crashed and won't restart. Can someone help me understand what the problem is?
Can someone help me understand how to make Ryan Bate's screencast on Backbone.js work
Can someone help me understand what SendGrid actually adds to an application architecture? Is
Can someone help me understand the WPF stretch alignment. I often run into issues
Can someone help me understand what's wrong with this query: DELETE FROM noteproject INNER

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.