From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Hongxu Chen Newsgroups: gmane.emacs.help Subject: Re: add-to-list with lexical variables Date: Sun, 09 Jun 2013 11:18:33 +0800 Message-ID: <87a9n02cqe.fsf@gmail.com> References: <87txl8u68b.fsf@gmail.com> <87r4gcln2h.fsf@kuiper.lan.informatimago.com> <8738ssledi.fsf@kuiper.lan.informatimago.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1370747935 6455 80.91.229.3 (9 Jun 2013 03:18:55 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 9 Jun 2013 03:18:55 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: "Pascal J. Bourguignon" Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Jun 09 05:18:56 2013 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 1UlW9g-0005gR-7f for geh-help-gnu-emacs@m.gmane.org; Sun, 09 Jun 2013 05:18:56 +0200 Original-Received: from localhost ([::1]:57186 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UlW9f-0007P3-Me for geh-help-gnu-emacs@m.gmane.org; Sat, 08 Jun 2013 23:18:55 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:35028) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UlW9T-0007Oy-0s for help-gnu-emacs@gnu.org; Sat, 08 Jun 2013 23:18:45 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UlW9Q-00013w-4y for help-gnu-emacs@gnu.org; Sat, 08 Jun 2013 23:18:42 -0400 Original-Received: from mail-pb0-x22e.google.com ([2607:f8b0:400e:c01::22e]:37318) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UlW9P-00013s-Rr for help-gnu-emacs@gnu.org; Sat, 08 Jun 2013 23:18:40 -0400 Original-Received: by mail-pb0-f46.google.com with SMTP id rq2so1056607pbb.19 for ; Sat, 08 Jun 2013 20:18:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type:content-transfer-encoding; bh=a4FtokneS7HVQCZcQamvWQ/DF+ZcKK4KDTZCTxR8L6s=; b=mKP+xFcEdww6VIHrQsFTzEC7aXySon2JtP35Xzmx7FuxTxxbO+9nNdPl1aDbdHhqKu 0C3m6ELSaUddj27PHVnyIB7qehL5TMKiDfbbnFHb19cZvaQqksthHcsPLoTcS+TE70+B 7QHYv+ck2GYaST13mX5k+v7lljESeBbdvygHAZbHNBeBHB4XyFB1QqqY2AUItzkqQDkw V/e561e8hQ+N23Ba9rtQ9CfN7MExzArID69RLxaAEJvK9ickNrGn3ORzYY2tks3K+ELh O3t95CDxttlctA5FOjytkNvorTPvN69ymV1Rh2KXyP4m93qmYu9aniqQm/wkMhkBfVF5 rfcQ== X-Received: by 10.68.170.37 with SMTP id aj5mr4753564pbc.77.1370747918420; Sat, 08 Jun 2013 20:18:38 -0700 (PDT) Original-Received: from hongxuchen-VM.STAP ([167.220.232.198]) by mx.google.com with ESMTPSA id lq4sm9629896pab.19.2013.06.08.20.18.36 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Sat, 08 Jun 2013 20:18:37 -0700 (PDT) In-Reply-To: <8738ssledi.fsf@kuiper.lan.informatimago.com> (Pascal J. Bourguignon's message of "Sat, 08 Jun 2013 19:08:09 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400e:c01::22e 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:91405 Archived-At: Thanks Pascal and Stefan! It's so kind of you to tell me so much about the scope/binding. It's much clear to me now(although I think I need to read your words and do some practice to fully grasp it). "Pascal J. Bourguignon" writes: > Hongxu Chen writes: > >> However I still don't know the difference `lexical-binding' and >> `lexical-let' brings. Are there some authoritative introductions/tutoria= ls? > > http://www.emacswiki.org/emacs/DynamicBindingVsLexicalBinding > > Also, any Common Lisp tutorial covers them.=20=20 > > > You may be interested by my explanations in the context of Common Lisp: > https://groups.google.com/group/comp.lang.lisp/msg/58f1c7ed53d0c0d6 > > > But basically, it's a question of time vs. space. > > > Dynamic is change over time. So dynamic binding will be concerned by > the time, by WHEN things are bound and referenced. > > > Lexical is textual. So lexical binding will be concerned by space, by > WHERE in the text things are bound and referenced. > > > With dynamic binding, things occur at certain times: > #+BEGIN_SRC elisp=20=20 > ;; with lexical-binding set to nil: > (let ((list (list 1 2 3))) > (add-to-list 'list 0)) > #+END_SRC > > time > ---- > > 1. the function list is called with arguments 1 2 and 3. > > 2. it returns a list: (1 2 3). > > 3. let creates a variable, it saves any dynamic binding the symbol > list may have, and it names the variable with the symbol list, and > it binds it (dynamically, at this time 3.) to the list (1 2 3). > > 4. (quote list) is evaluated. > > 5. it returns the symbol list. > > 6. 0 is evaluated. > > 7. it returns 0 > > 8. the function add-to-list is called with as arguments, the symbol > list and the integer 0. > > 9. the function changes the dynamic binding of the symbol list. > remember at this time, while the function add-to-list is being > called, there symbol list is dynamically bound to the list (1 2 3), > since the time 3. > > 10. it returns, and the dynamic binding to the variable list has been > changed. > > 11. let destroys the dynamic binding of list, and destroys the variable > named list. Whatever dynamic binding list had before is restored. > >=20=20 > > With lexical binding, what matters is the space, the place where > expressions are: > #+BEGIN_SRC elisp=20=20 > ;; with lexical-binding set to t: > (let ((list (list 1 2 3))) > (add-to-list 'list 0)) > #+END_SRC > > (let ((list (list 1 2 3))) > ;; From HERE > (add-to-list 'list 0) > ;; To HERE, there is a lexical variable named list. > ) > > (defun add-to-list (variable value) > ;; From HERE > =E2=80=A6 > =E2=80=A6 code of add-to-list > =E2=80=A6 > ;; To HERE, we're outside the lexical scope of the let form above.=20 > ;; There is no lexical variable named list here! Only the variables > ;; and parameters of add-to-list. > ) > > So there's no way for add-to-list to access directly the lexical > variable list defined in the let form.=20=20 > > It could access it indirectly, if you passed it a closure. See for > example: > http://www.informatimago.com/articles/usenet.html#C-like-pointers-in-Lisp > (those CL examples should run the same in emacs lisp with > lexical-binding set to t). > > > But since add-to-list is predefined not to use a closure, but to modify > a dynamic binding, it won't change a lexical binding. It's not it's > purpose, it's not what it's designed to do. --=20 Regards, Hongxu Chen