From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Joost Kremers Newsgroups: gmane.emacs.help Subject: Re: How does letf work? Date: 29 Jan 2014 08:37:45 GMT Message-ID: References: <52E838C8.5020101@miszellen.de> NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1390984811 10761 80.91.229.3 (29 Jan 2014 08:40:11 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 29 Jan 2014 08:40:11 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Jan 29 09:40:19 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1W8Qh0-0005IM-C3 for geh-help-gnu-emacs@m.gmane.org; Wed, 29 Jan 2014 09:40:18 +0100 Original-Received: from localhost ([::1]:40944 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W8Qgz-0008G6-VS for geh-help-gnu-emacs@m.gmane.org; Wed, 29 Jan 2014 03:40:17 -0500 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 73 Original-X-Trace: individual.net TbFZT7rUPQ2FbdUIdvdXVgOMYUZplCYVTYgvxDBE8Pd7/0tQN5 Cancel-Lock: sha1:cIp02ql8e7uKkh0SogY/t1Og3U8= Mail-Copies-To: nobody X-Editor: Emacs of course! User-Agent: slrn/pre1.0.0-18 (Linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:203477 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:95746 Archived-At: Michael Heerdegen wrote: > Florian Beck writes: > >> (letf (((cdr test-x) '(a b c d))) >> test-x) >> >> = > (KEY 1 2 3 4) > > Though paradoxical, it seems right to me. You return a list that you > modified temporarily, but when the result is printed in the echo area, > the letf has been left and the structure of the list has changed back. In essence, yes, but it's not as simple as all that: (letf (((cdr test-x) '(a b c d))) (cdr test-x)) = > (KEY a b c d) Taking what you say at face value, one might expect that at the moment the value of (cdr test-x) is printed, it's already been changed back to the original value, so it should say (KEY 1 2 3 4), but it doesn't. (I think that in a way, you are in fact correct, but the issue is more complicated than your formulation suggests; see below.) > Note that the variable test-x is not shadowed by your letf: > > (let ((test-x-before test-x)) > (letf (((cdr test-x) '(a b c d))) > (eq test-x test-x-before))) > >==> t No, it *is* shadowed (more correctly, its cdr is shadowed): (let ((test-x-before test-x)) (letf (((cdr test-x) '(a b c d))) (cdr test-x-before))) => (a b c d) test-x and test-x-before are `eq`, which means they point to the (token-)identical object in memory. I suspect what is going on is that what is returned by the letf (and cl-letf) is not the value of the symbol test-x (which I've kinda assumed up until now), but a pointer to the object test-x is referring to (i.e., the first cons cell of the list). If you would shadow test-x itself, this object is different from the object the global test-x binding points to, and so that object is printed. (Even though the printing is done after the letf has returned, since there is still a pointer to the object, the object itself is kept alive until it's been printed.) However, in the OP's example, it's not the binding of test-x that is shadowed, but its cdr. So inside the letf, test-x still points to the same (token-identical) cons cell as the global binding of test-x, it's just that that cons cell has a shadowed cdr. What is returned by the letf is a pointer to that object. In this case, when the letf returns, there is no longer any pointer to the shadowed cdr, so it is discarded. 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. 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. If it does, the object can stay around longer, as demonstrated by returning the (cdr test-x). -- Joost Kremers joostkremers@fastmail.fm Selbst in die Unterwelt dringt durch Spalten Licht EN:SiS(9)