Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Taking a look at how the existing one (in SBCL, at least) does things, I see:</p> <pre><code>* (macroexpand-1 '(push 1 *foo*)) (LET* ((#:G823 1) (#:NEW822 (CONS #:G823 *FOO*))) (SETQ *FOO* #:NEW822)) T </code></pre> <p>So, I imagine, mixing in a combination of your version and what this generates, one might do:</p> <pre><code>(defmacro my-push (element place) (let ((el-sym (gensym)) (new-sym (gensym "NEW"))) `(let* ((,el-sym ,element) (,new-sym (cons ,el-sym ,place))) (setq ,place ,new-sym))))) </code></pre> <p>A few observations:</p> <ol> <li><p>This seems to work with either <code>setq</code> or <code>setf</code>. Depending on what problem you're actually trying to solve (I presume re-writing <code>push</code> isn't the actual end goal), you may favor one or the other.</p></li> <li><p>Note that <code>place</code> does still get evaluated twice... though it does at least do so only after evaluating <code>element</code>. Is the double evaluation something you actually need to avoid? (Given that the built-in <code>push</code> doesn't, I'm left wondering if/how you'd be able to... though I'm writing this up before spending terribly much time thinking about it.) Given that it's something that needs to evaluate as a "<a href="http://clhs.lisp.se/Body/26_glo_p.htm#place" rel="nofollow">place</a>", perhaps this is normal?</p></li> <li><p>Using <code>let*</code> instead of <code>let</code> allows us to use <code>,el-sym</code> in the setting of <code>,new-sym</code>. This moves where the <code>cons</code> happens, such that it's evaluated in the first evaluation of <code>,place</code>, and after the evaluation of <code>,element</code>. Perhaps this gets you what you need, with respect to evaluation ordering?</p></li> <li><p>I think the biggest problem with your second version is that your <code>setf</code> really does need to operate on the symbol passed in, not on a <code>gensym</code> symbol.</p></li> </ol> <p>Hopefully this helps... (I'm still somewhat new to all this myself, so I'm making some guesses here.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload