[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How does letf work?
From: |
Michael Heerdegen |
Subject: |
Re: How does letf work? |
Date: |
Thu, 30 Jan 2014 00:46:42 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) |
Nicolas Richard <theonewiththeevillook@yahoo.fr> writes:
> How I understand it is : what is returned by the letf *is* the value of
> the symbol, but since it is a cons cell it can be modified by setf
> (using setcar and setcdr). So part of the job of letf is to *change* the
> cons cell upon exiting. The box is the same (and is what is returned),
> but the content was changed at the moment the last closing paren of letf
> is crossed. IOW:
A good summary, IMHO.
In LISP, lists are passed by reference. When a list is bound to a
variable, the list is not copied. The value referenced by the variable
will be the very same object.
> The object is the same, but its content changed. Similar to :
> (let ((foo (cons 'a 'b))
> (bar))
> (setq bar foo) ;; bar and foo have the same object in their value cell.
> (setcdr foo 'c) ;; change the cdr of that cons cell
> (eq bar foo)) ;; they're still the same.
> => t
>
> > 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
>
> I guess it's not gone per se until the garbage collector does its
> work.
(a b c d) is garbage collected. The object equal to (KEY a b c d)
inside letf is never destroyed or garbage collected, since this list is
referenced by test-x and the return value of the function. But a part
of the object has been changed (restored) by letf.
Regards,
Michael.