all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Michael Heerdegen <michael_heerdegen@web.de>
To: help-gnu-emacs@gnu.org
Subject: Re: Another question about lambdas
Date: Fri, 03 Feb 2023 01:20:19 +0100	[thread overview]
Message-ID: <87h6w3bnwc.fsf@web.de> (raw)
In-Reply-To: <87357rnch0.fsf@dataswamp.org> (Emanuel Berg's message of "Mon, 30 Jan 2023 18:44:59 +0100")

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	other threads:[~2023-02-03  0:20 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-10 17:23 Another question about lambdas Michael Heerdegen
2022-12-10 19:33 ` tomas
2022-12-10 20:29   ` Michael Heerdegen
2022-12-10 20:03 ` Emanuel Berg
2022-12-12  1:50   ` Michael Heerdegen
2023-01-18 11:52     ` Emanuel Berg
2023-01-19 13:23       ` Michael Heerdegen
2022-12-10 21:58 ` Arash Esbati
2022-12-11  0:00   ` Michael Heerdegen
2022-12-11  0:09     ` Michael Heerdegen
2022-12-11  7:02     ` tomas
2022-12-12  1:56       ` Michael Heerdegen
2022-12-12  5:24         ` tomas
2022-12-12  6:22           ` Michael Heerdegen
2023-01-18 11:51             ` Emanuel Berg
2023-01-19 13:24               ` Michael Heerdegen
2023-01-19 13:38               ` Michael Heerdegen
2023-01-30 17:44                 ` Emanuel Berg
2023-02-03  0:20                   ` Michael Heerdegen [this message]
2023-02-25  7:31                     ` Emanuel Berg
2023-02-27  8:28                       ` tomas
2023-01-18 11:47           ` Emanuel Berg
2023-01-18 11:33         ` Emanuel Berg
2023-01-19 13:32           ` Michael Heerdegen
2022-12-13 11:26     ` Arash Esbati
2022-12-14  2:22       ` Michael Heerdegen
2022-12-11  0:13 ` Gregory Heytings
2022-12-11  0:38   ` Michael Heerdegen

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

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

  git send-email \
    --in-reply-to=87h6w3bnwc.fsf@web.de \
    --to=michael_heerdegen@web.de \
    --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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.