From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Newsgroups: gmane.emacs.help Subject: Re: Lexical vs. dynamic: small examples? Date: Sat, 14 Aug 2021 22:41:04 +0200 Message-ID: <20210814204104.GB14760@tuxteam.de> References: <4a9bddb9ec57299b3b0c@heytings.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="NDin8bjvE/0mNLFQ" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="1326"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/1.5.21 (2010-09-15) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sat Aug 14 22:42:42 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 1mF0UM-00006o-Sj for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 14 Aug 2021 22:42:42 +0200 Original-Received: from localhost ([::1]:40184 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mF0UL-0002uf-RT for geh-help-gnu-emacs@m.gmane-mx.org; Sat, 14 Aug 2021 16:42:41 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:35808) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mF0Sp-0002m3-5s for help-gnu-emacs@gnu.org; Sat, 14 Aug 2021 16:41:07 -0400 Original-Received: from mail.tuxteam.de ([5.199.139.25]:37369) by eggs.gnu.org with esmtps (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.90_1) (envelope-from ) id 1mF0Sn-0002lp-Kj for help-gnu-emacs@gnu.org; Sat, 14 Aug 2021 16:41:06 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tuxteam.de; s=mail; h=From:In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:To:Date; bh=UXTjblT5E7LyFJk8kvXnjuAIam0SmX1e7gYplAUZprw=; b=ELaSivxsywTgORNJftatG2xauwZ7HrlY/Qmnqaf9NZdayiPSVnstd2EKF1be1y5cvA+vI33kG0ti49h1Ww59Dj7bR1MgZ5tJ2N8DBwY/NvUXhSkhqDqNwS/Sa3qF7IN9gY1wOBenCRKSjUZuXqu95j5FsdpoRuU7aYCF3ygstxjbxstC/4DkgEvYPf8tWZPSzPP/bc7gBZ8Eu0b0XqUF9lxSF1sSkPSA41BFFZIsJi45Oj0st/MuT2ghn46x7+S6krM/VDnPxKlN/PF47JXzWEFpXXyR/77jR6sQuSm1xyuHxCEnrOqPLj8NsXMxVghgcyqZqJqOdZr9n6/9zPzfPQ==; Original-Received: from tomas by mail.tuxteam.de with local (Exim 4.80) (envelope-from ) id 1mF0Sm-0004JX-4K for help-gnu-emacs@gnu.org; Sat, 14 Aug 2021 22:41:04 +0200 Content-Disposition: inline In-Reply-To: <4a9bddb9ec57299b3b0c@heytings.org> Received-SPF: pass client-ip=5.199.139.25; envelope-from=tomas@tuxteam.de; helo=mail.tuxteam.de X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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:132546 Archived-At: --NDin8bjvE/0mNLFQ Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sat, Aug 14, 2021 at 07:00:29PM +0000, Gregory Heytings wrote: >=20 > > > >I am trying to write a section on lexical vs. dynamic binding for > >a tutorial on Emacs Lisp, and I am looking for very short demos > >that show how things work differently in dynamic and in lexical > >binding... > > >=20 > Suppose you write a function to remove unnecessary whitespaces at > the end of lines: >=20 > (defun delete-whitespace-at-eol () > (interactive) > (save-excursion (replace-regexp " *$" "" nil (point-min) (point-max)))) This is a good example. There are other ones. Basically, whenever you want some "dynamic state" to be in effect for the whole "call tree" below you. Typically those are well-known things with an agreed-upon purpose. My favourite example is some kind of "debug setting" (a function, a value, whatever). There's a reason why the convention in CL is to have those variable named with asterisks around, like *this*. (I had many arguments whithin Perl. With the arrival of lexical variables, there was a fraction which tried to convince people to /never/ use dynamic variables, and I tried to explain that, yes, use sparingly, but sometimes they can make code clearer. Cheers - t --NDin8bjvE/0mNLFQ Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iEYEARECAAYFAmEYKmAACgkQBcgs9XrR2kb9kQCeL/Samp8KbathAw4P/0Rg8xq1 uFoAnjEgF12gs4K07cCeC2UW6oYov4Hz =Q3/c -----END PGP SIGNATURE----- --NDin8bjvE/0mNLFQ--