From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: sending function arguments to recursive function calls Date: Sun, 19 May 2013 22:59:41 +0200 Organization: Informatimago Message-ID: <878v3aofcy.fsf@kuiper.lan.informatimago.com> References: <0F54256BD7B94384AC4DDA919D502C20@us.oracle.com> <4D1DF48A7223443FA454C07B20B80E21@us.oracle.com> <87mwrt7py6.fsf@yandex.ru> <2AC6E6871319483CABA0387DEF8B39CC@us.oracle.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1369007470 2377 80.91.229.3 (19 May 2013 23:51:10 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 19 May 2013 23:51:10 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon May 20 01:51:11 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 1UeDNd-0003eY-NB for geh-help-gnu-emacs@m.gmane.org; Mon, 20 May 2013 01:51:09 +0200 Original-Received: from localhost ([::1]:55926 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UeDNd-0006T1-CP for geh-help-gnu-emacs@m.gmane.org; Sun, 19 May 2013 19:51:09 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 116 Original-X-Trace: individual.net DdijGWmpOWfzBcxjRUE1mQ8rvg4kGw3RkWKG2igiKGserSHuWg Cancel-Lock: sha1:M2IyNzAwMDA2MTkwNDY3ZDE0OWU1MTc1NWE0MzIyOGFhYWI5OWM2Zg== sha1:wKbmtUmJrvnadrLJrRNG/uHVqRc= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:198656 X-Mailman-Approved-At: Sun, 19 May 2013 19:50:34 -0400 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:90921 Archived-At: Dmitry Gutov writes: > On 17.05.2013 18:31, Drew Adams wrote: >>> This is the ugly side of dynamic scoping. >>> (defun foo () (let ((bar 42)) (baz))) >>> (defun baz () bar) >>> (foo) ; => 42 >>> baz ; => void-variable error >> >> Huh? I guess you meant to write >> (baz) ; => "void-variable bar" error > > No, I meant exactly to write variable reference, not function call, to > illustrate that it's not defined globally. Then you meant to write: bar not: baz >> There is nothing ugly about that behavior. > > It's ugly because this kind of code is hard to reason about and, > consequently, hard to modify. Suppose I want to rewrite `foo' (and > suppose it's longer than this one line). Yes. That's why you should adopt the CL convention of naming all the special variables (those with dynamic binding) with stars: (defun foo () (let ((*bar* 42)) (declare (special *bar*)) (baz))) (defun baz () (declare (special *bar*)) *bar*) (foo) ; => 42 *bar* ; Debugger entered--Lisp error: (void-variable *bar*) > Can I rename `bar' to something else? Yes. You should name it *bar*, and declare it special locally. Right, for now (declare (special *bar*)) has no effect in emacs lisp since it's the default, but it states your intent! > No idea: to be absolutely sure, > I have to search the definitions of all functions that `foo' calls, > and if I find a `bar' reference in any of them, I'll now have to > search for any other functions that call them, etc. IOW, this makes > for terrible composability. Definitely. That's why the default is lexical binding, and you have to declare specially variables with dynamic binding, either with declare special, or globally with defvar or defparameter. > The behavior is ugly because it allows the code to be written this way. > > A worse example is when `bar' is one of the arguments to `foo' (ugh). Global or local special declarations are still possible, even for parameters. >> The `let' binds variable `bar' for the dynamic extent of the call to `foo'. >> There is no other binding of `bar' or assignment to it here, so `(baz)' refers >> to an unbound variable `bar'. >> >> What happens with lexical scoping? >> (foo) ; => "void-variable bar" error >> (baz) ; => "void-variable bar" error > > As it should. Contrast this with the situation when `bar' has been > defvar'ed in advance. Both functions would know that this var is > global, so if it's renamed in some place, it definitely should be > renamed in all functions that reference it. > > This is what I can call the light side of dynamic scoping, and it's > how the term "dynamic binding" is often defined. > >> Dynamic binding facilitates user extension ("monkey patching"). And yes, this >> is particularly important for a dynamic user environment like Emacs. >> >> It is easy to find references lauding the benefits of lexical binding (most >> languages use only lexical binding). Stallman explains well why dynamic binding >> is important for Emacs: >> http://www.gnu.org/software/emacs/emacs-paper.html#SEC17. > > I could offer some criticism for the paper, but there's really no need. > > Just recall that Emacs is on track to eventually replace dynamic > scoping with lexical scoping everywhere, with exceptions for defvar'ed > vars (controlled dynamic binding), and nobody is really arguing that > Emacs will become too hard to customize as a result. Nobody reasonably > well-informed, anyway. Of course. As long as dynamic binding is still available for the few cases where it's needed, as in Common Lisp. -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}. You can take the lisper out of the lisp job, but you can't take the lisp out of the lisper (; -- antifuchs