From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.help Subject: Re: [External] : How to create a higher order function? Date: Fri, 24 Sep 2021 11:04:08 +0200 Message-ID: <87czoyfipj.fsf@mbork.pl> References: <87k0jawotx.fsf@mbork.pl> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="6510"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.1.0; emacs 28.0.50 Cc: Help Gnu Emacs mailing list To: Drew Adams Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Fri Sep 24 11:08:08 2021 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mThBf-0001QO-Qw for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 24 Sep 2021 11:08:07 +0200 Original-Received: from localhost ([::1]:32948 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mThBe-0005EQ-IB for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 24 Sep 2021 05:08:06 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:35814) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mTh7v-0001we-2G for help-gnu-emacs@gnu.org; Fri, 24 Sep 2021 05:04:15 -0400 Original-Received: from mail.mojserwer.eu ([195.110.48.8]:34562) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mTh7t-0001f9-1j for help-gnu-emacs@gnu.org; Fri, 24 Sep 2021 05:04:14 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id CE275E6DB1; Fri, 24 Sep 2021 11:04:11 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mail.mojserwer.eu Original-Received: from mail.mojserwer.eu ([127.0.0.1]) by localhost (mail.mojserwer.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Q3e4Se2-i7OQ; Fri, 24 Sep 2021 11:04:08 +0200 (CEST) Original-Received: from localhost (178235147106.dynamic-3-poz-k-0-1-0.vectranet.pl [178.235.147.106]) by mail.mojserwer.eu (Postfix) with ESMTPSA id 8AB79E6208; Fri, 24 Sep 2021 11:04:08 +0200 (CEST) In-reply-to: Received-SPF: pass client-ip=195.110.48.8; envelope-from=mbork@mbork.pl; helo=mail.mojserwer.eu X-Spam_score_int: -5 X-Spam_score: -0.6 X-Spam_bar: / X-Spam_report: (-0.6 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URI_DOTEDU=1.997 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 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-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:133212 Archived-At: On 2021-09-21, at 17:49, Drew Adams wrote: >> My question is: which of (B) and (C) is better? (C) is definitely >> simpler, and with lexical scope becoming the default (perhaps at some >> point in time in the future) or the recommended (even now, I guess) >> setting, is probably the way to go. But maybe (B) is better under some >> circumstances? Is it faster? (I don't think so, and my simple >> benchmarks were inconclusive.) Is it more memory-efficient? (Could >> be.) Does it have any other advantage? > > (C) is definitely better, in general. > > (That's really the takeaway answer.) > > The lexical environment, i.e., the one where > the function is defined, is the environment you > generally want for the function - it's, well, > the environment that _defines_ the function. > > Generally speaking that's the only proper > environment for defining the function. Of > course, this is Lisp, and nothing says you're > obliged to use the "proper", static, definition > of anything. > ___ > > Two counter cases to (C), of sorts: > > 1. If you specifically want to allow other code > to be able to _change the behavior_ of the > function by simply providing an arbitrary > runtime value for some variable, then you might > want to dynamically bind that variable. > > 2. If a free variable in the function body is > never used _as a variable_, so that only its > _value_ at the point of the function definition > is needed, then you can do what you did, which > is to just substitute the value for the var. > > This means there's no carrying around that var > binding in the closure, no need to look up its > value, etc. But see next: that generally > doesn't mean a benefit in performance - the > contrary. > > Wrt #2: Be aware that, at least with current > Elisp and its byte-compiler, #2 essentially > _loses_ the function definition at the point > where it's defined. Instead, it substitutes > a _list_, a constructed lambda form, for the > function. It's only later, in some other > context/environment, that that list can get > interpreted as a function. > > Among other things, this means that that > representation of a function can't be compiled > as a function. It can only be interpreted > (any number of times, in any contexts). > > It can't in any way be treated or understood > by Emacs as a _function_ - it's just a list > with a lambda form, until it's eval'd. Put > differently, it's just a _mention_ till it's > explicitly _used_. It's a template function > definition, to be filled in and interpreted > as a function wherever and whenever. > > On the other hand, for better and worse, it > can be treated as a list (it is one). IOW, > instead of a function, you have essentially > the _name_, a representation, of a function. > If you want to do things to or with such a > representation, you can. But this is rare. > ___ > > Wrt the advantages and uses of dynamic binding > in the context of _Emacs_ - i.e., the reason > why Emacs has, and should continue to have, > dynamic binding (along with lexical binding), > see RMS's arguments here: > > https://www.gnu.org/software/emacs/emacs-paper.html#SEC17 > > https://www.gnu.org/software/emacs/emacs-paper.html#SEC18 Yes, I am aware of that, thanks! > Those arguments are as valid today (& tomorrow) > as they were when written in 1981. > > Wrt the uses and behavior of dynamic & lexical > binding in _Lisp_ (not particular to Emacs), I > recommend reading the relevant parts of "Common > Lisp The Language". > > https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node43.html > > Here's my SO post on reading that section in > relation to Emacs: > > https://stackoverflow.com/a/7135315/729907 Thanks, I'll look into those, too! Best, -- Marcin Borkowski http://mbork.pl