[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Question about let binding behavior
From: |
tomas |
Subject: |
Re: Question about let binding behavior |
Date: |
Tue, 8 Oct 2024 08:41:42 +0200 |
On Tue, Oct 08, 2024 at 06:21:03AM +0000, Louis-Guillaume Gagnon wrote:
> Dear all,
>
> I'm a long time user but I've only recently started to dive into elisp
> programming. I'm a bit surprised by the following behavior.
>
> Let's say I write the following silly function:
>
> (defun foo (bar)
> (let ((baz '((quux . 0) (quuz . 0))))
> (if (> 10 bar)
> (setcdr (assoc 'quux baz) bar)
> (setcdr (assoc 'quuz baz) bar))
> baz))
>
> Then the following evals:
>
> (foo 1)
> => ((quux . 1) (quuz . 0))
>
> (foo 100)
> => ((quux . 1) (quuz . 100))
>
> I'm surprised that the list is not reset since I thought the binding was
> local to the function, but clearly I don't have the right mental model
> for elisp evaluation.
The binding (i.e. the "thing" linking the symbol baz to the value
'(...) is local. But what you do with that (setcdr... ) is to change
the value itself. Your function returns this (possibly changed) value.
Lisps have a different "accent" than more pure functional languages,
where values are more immutable.
> For the actual application I ended up using
> copy-tree but I'd like to understand this better -- could someone
> explain or point out the relevant sections of the manual?
Did that help?
Cheers & enjoy Lisp :-)
--
tomás
signature.asc
Description: PGP signature
- Question about let binding behavior, Louis-Guillaume Gagnon, 2024/10/08
- Re: Question about let binding behavior,
tomas <=
- Re: Question about let binding behavior, Louis-Guillaume Gagnon, 2024/10/08
- Re: Question about let binding behavior, tomas, 2024/10/08
- Re: Question about let binding behavior, tomas, 2024/10/08
- Re: Question about let binding behavior, Louis-Guillaume Gagnon, 2024/10/09
- Re: Question about let binding behavior, Tomas Hlavaty, 2024/10/09
- Re: Question about let binding behavior, Stefan Monnier, 2024/10/09
- Re: Question about let binding behavior, Tomas Hlavaty, 2024/10/10
- Re: Question about let binding behavior, Stefan Monnier, 2024/10/10
- Re: Question about let binding behavior, Rudolf Schlatte, 2024/10/18
- Re: Question about let binding behavior, Joost Kremers, 2024/10/08