help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question about let binding behavior


From: Tomas Hlavaty
Subject: Re: Question about let binding behavior
Date: Thu, 10 Oct 2024 09:58:31 +0200

On Wed 09 Oct 2024 at 19:39, Stefan Monnier via Users list for the GNU Emacs 
text editor <help-gnu-emacs@gnu.org> wrote:
>> or you can avoid the constant in the first place, for example with
>> minimal change like this:
>>
>>    (let ((baz `((quux . 0) (quuz . 0))))
>
> Hmm... I think this makes no difference: ` does not guarantee it returns
> a different object each time.
> And indeed, if you try:
>
>     (macroexpand '`((quux . 0) (quuz . 0)))
> =>
>     '((quux . 0) (quuz . 0))

ok, try (let ((baz `((quux . ,0) (quuz . ,0))))

(macroexpand '`((quux . ,0) (quuz . ,0)))
=>
(list (cons 'quux 0) (cons 'quuz 0))

although sbcl does not get tricked and requires

CL-USER> (macroexpand '`((quux . ,a) (quuz . ,b)))
(LIST (LIST* 'QUUX A) (LIST* 'QUUZ B))
T

(defun foo2 (bar &optional (a 0) (b 0))
  (let ((baz `((quux . ,a) (quuz . ,b))))
    (if (> 10 bar)
        (setf (cdr (assoc 'quux baz)) bar)
        (setf (cdr (assoc 'quuz baz)) bar))
    baz))
(foo2 1)
(foo2 100)

in emacs-lisp:

(defun foo2 (bar &optional a b)
  (let ((baz `((quux . ,(or a 0)) (quuz . ,(or b 0)))))
    (if (> 10 bar)
        (setcdr (assoc 'quux baz) bar)
      (setcdr (assoc 'quuz baz) bar))
    baz))
(foo2 1)
(foo2 100)

although this also works in emacs-lisp (for now?):

(defun foo3 (bar)
  (let* ((a 0)
         (b 0)
         (baz `((quux . ,a) (quuz . ,b))))
    (if (> 10 bar)
        (setcdr (assoc 'quux baz) bar)
      (setcdr (assoc 'quuz baz) bar))
    baz))
(foo3 1)
(foo3 100)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]