help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Another question about lambdas


From: Michael Heerdegen
Subject: Re: Another question about lambdas
Date: Fri, 03 Feb 2023 01:20:19 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

Emanuel Berg <incal@dataswamp.org> writes:

> Are we talking lexical/static `let' inside or outside
> `defun's?
>
> Inside defuns they are very practical, local variables,
> basically, outside defuns they are interesting, rather, I have
> found 2 use cases so far, first, what in other languages are
> refered to as static variables (whose value don't reset
> between calls), second, the possibility to share such
> variables between functions.
>
> This file demonstrate those use cases, other than that I don't
> know what one is supposed to do with them, really, and so far
> they don't do anything global variables don't. So they are
> a method to not have those, basically?

Dunno if this is a good summary, I would be careful.  I find the
question a bit artificial - how is the classification global vs local
functions motivated?  The top-level lexical environment is empty, so
top-level defuns are not interesting as closures.

So we are actually only speaking about the case of functions defined
anywhere besides top-level that are given a global name.  In a
functional language functions with a globally accessible name that have
an alterable state are probably not that sexy - we are instead trying to
make those functions pure, or try to restrict side effects to obvious
well-defined things like creating a window.

Useful cases that come to my mind are more of kind where the state is
never altered but lexical binding eases the definition of bunches of
functions - in a very simple case e.g.

#+begin_src emacs-lisp
(defun my-make-adder (n)
  (lambda (m) (+ m n)))

(defalias 'my-1+ (my-make-adder 1))
(defalias 'my-1- (my-make-adder -1))
#+end_src


Such lexical variables are still different from special top-level
variables (`defvar's): They are private (not accessible "from the
outside").  You can have several of them having the same name.  They
don't clobber a name space.  And no binding created in any context
outside of the lexical context where the functions are defined can ever
change the value of those variables.


Michael.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]