unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: sending function arguments to recursive function calls
Date: Sun, 19 May 2013 22:59:41 +0200	[thread overview]
Message-ID: <878v3aofcy.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.70.1368982677.22516.help-gnu-emacs@gnu.org

Dmitry Gutov <dgutov@yandex.ru> 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


  parent reply	other threads:[~2013-05-19 20:59 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-04 13:01 sending function arguments to recursive function calls Gauthier Östervall
2013-05-04 15:30 ` Drew Adams
2013-05-07 11:25   ` Gauthier Östervall
2013-05-07 14:04     ` Drew Adams
2013-05-08 12:21       ` Stefan Monnier
2013-05-09  8:35         ` Gauthier Östervall
2013-05-09 12:23           ` Stefan Monnier
2013-05-12 13:19             ` Gauthier Östervall
2013-05-13 14:55               ` Stefan Monnier
2013-05-17 12:20                 ` Gauthier Östervall
2013-05-17 12:26                   ` Dmitry Gutov
2013-05-17 14:31                     ` Drew Adams
2013-05-19 16:57                       ` Dmitry Gutov
2013-05-21 16:34                         ` Drew Adams
     [not found]                       ` <mailman.70.1368982677.22516.help-gnu-emacs@gnu.org>
2013-05-19 20:59                         ` Pascal J. Bourguignon [this message]
2013-05-20 19:31                           ` Dmitry Gutov
     [not found]                           ` <mailman.94.1369078320.22516.help-gnu-emacs@gnu.org>
2013-05-20 19:55                             ` Pascal J. Bourguignon
2013-05-07 14:32     ` Pascal J. Bourguignon
     [not found]     ` <mailman.25279.1367935468.855.help-gnu-emacs@gnu.org>
2013-05-07 14:55       ` Pascal J. Bourguignon
2013-05-08 12:25         ` Stefan Monnier
2013-05-05  1:22 ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=878v3aofcy.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).