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

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

Re: How does letf work?


From: Florian Beck
Subject: Re: How does letf work?
Date: Wed, 29 Jan 2014 16:29:56 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0

Thanks for the answers!


On 29.01.2014 10:14, Joost Kremers wrote:
And since the printing is done outside the letf, as you pointed
out, the object that's printed is the one pointed to by the global
binding of test-x.

Well, there is no local binding for test-x. As far as I understand what
happens is this:

(defvar test-x '(KEY 1 2 3 4))

(letf (((cdr test-x) '(a b c d)))
  test-x)

This returns a pointer to test-x, but its cdr has been restored before
the value is printed.

But that's not because outside the letf the object created inside
it is necessarily gone. It's gone because letf doesn't return a
pointer to it.

But it does return a pointer to test-x, which in turn pointed to the
cdr. This is exactly what happens in the second example:

(let ((x (copy-list test-x)))
  (setf (cdr x) '(a b c d))
  x)

Because let creates a new binding (for x), a new pointer is created.
With copy-list, a new object is created to which this binding points.
After modifying the cdr of that object, the pointer is returned.
Outside the let, the binding for x is gone but the pointer to the
object it referred to is still alive, so the object itself is also
kept alive (until it's been printed).

This makes sense. But it cannot be the whole story:

(letf* ((x (copy-list  test-x))
        ((cdr x) '(a b c d)))
  x)

=> (KEY 1 2 3 4)

I'm still confused.

--
Florian Beck



reply via email to

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