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: Louis-Guillaume Gagnon
Subject: Re: Question about let binding behavior
Date: Wed, 09 Oct 2024 08:01:37 +0000

Hi Tomas,

Le 10/8/24 à 6:02 PM, tomas@tuxteam.de a écrit :

> If a program attempts to change objects that should not be changed,
>   the resulting behavior is undefined: the Lisp interpreter might signal
>   an error, or it might crash or behave unpredictably in other ways.(1)
>
>      When similar constants occur as parts of a program, the Lisp
>   interpreter might save time or space by reusing existing constants or
>   their components.  For example, ‘(eq "abc" "abc")’ returns ‘t’ if the
>   interpreter creates only one instance of the string literal ‘"abc"’, and
>   returns ‘nil’ if it creates two instances.  Lisp programs should be
>   written so that they work regardless of whether this optimization is in
>   use.

So IIUC when I write "(let ((baz '((quux . 0) (quuz . 0)))) [...]", I'm 
creating a constant & binding it to 'baz'. The interpreter is free to use the 
same underlying list whenever I refer to this constant, & in this case, it 
survives across calls to the function. My program relied on modifying the 
constant and expecting those changes not to persist between calls, which means 
the program is ill-formed. By using "copy-tree" on the constant, I'm 
instructing the interpreter to give me a fresh copy of the constant at every 
call, which is required to get the behavior I expect.

Thanks for pointing out the section, I'll definitely read it

Cheers
L-G

reply via email to

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