From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jean Louis Newsgroups: gmane.emacs.help Subject: Re: Advantage using mapc over dolist Date: Tue, 3 Dec 2024 22:24:56 +0300 Message-ID: References: <87zflevbwm.fsf@neko.mail-host-address-is-not-set> <87frn5lswe.fsf@neko.mail-host-address-is-not-set> <875xo1lr1q.fsf@neko.mail-host-address-is-not-set> <87mshdrze6.fsf@neko.mail-host-address-is-not-set> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="5345"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/2.2.12 (2023-09-09) Cc: help-gnu-emacs@gnu.org To: Tomas Hlavaty Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Tue Dec 03 20:27:26 2024 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 1tIYYP-0001BF-Jq for geh-help-gnu-emacs@m.gmane-mx.org; Tue, 03 Dec 2024 20:27:25 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tIYXn-0003zJ-ST; Tue, 03 Dec 2024 14:26:47 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tIYXl-0003yq-5g for help-gnu-emacs@gnu.org; Tue, 03 Dec 2024 14:26:45 -0500 Original-Received: from stw1.rcdrun.com ([217.170.207.13]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tIYXj-0007pO-5L for help-gnu-emacs@gnu.org; Tue, 03 Dec 2024 14:26:44 -0500 Original-Received: from localhost ([::ffff:41.75.174.232]) (AUTH: PLAIN admin, TLS: TLS1.3,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by stw1.rcdrun.com with ESMTPSA id 0000000000081F0A.00000000674F5B70.00358B62; Tue, 03 Dec 2024 12:26:39 -0700 Mail-Followup-To: Tomas Hlavaty , help-gnu-emacs@gnu.org Content-Disposition: inline In-Reply-To: <87mshdrze6.fsf@neko.mail-host-address-is-not-set> Received-SPF: pass client-ip=217.170.207.13; envelope-from=bugs@gnu.support; helo=stw1.rcdrun.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_PASS=-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.29 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-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:148553 Archived-At: * Tomas Hlavaty [2024-12-03 10:36]: > On Tue 03 Dec 2024 at 09:13, Jean Louis wrote: > > * Tomas Hlavaty [2024-12-03 00:21]: > >> On Mon 02 Dec 2024 at 23:50, Jean Louis wrote: > >> >> hmm, somebody renamed it to cl-ecase and cl-case > >> >> similar to flet -> cl-flet and labels -> cl-labels > >> >> what a shame > >> > > >> > I find it liberating. > >> > >> What do you mean? > >> Could you explain that? > >> > >> Emacs is switching to lexical scope and degraded two of the most lexical > >> scope related things to secodary citizens. > > > > I am an Emacs Lisp programmer, and all Common Lisp functions prefixed with `cl-` I find liberating in the sense that personally within Emacs Lisp I do not like mixing it because it is not Common Lisp. All my software was first in Common Lisp, I know it and use it every day, I am a heavy user of my own Common Lisp. But within Emacs, I like using Emacs Lisp pureā€”it is a personal choice. But Tomas, I mentioned nothing about pcase. I said cl- namespace being separate feels liberating to me as my personal choice. If you wish to use those commands without cl-prefix, there is solution that Stefan wrote in recent email. > I do not understand this explanation. It feels like renaming car and > cdr because it feels liberating. It can be. Lisp is all about making it right for you. I actually like that `first' is now `cl-first', but if I wish to use it, I can simply alias it. You could make list and alias functions and you are fine. > Beginners have been nagging about this for maybe 60 years already. Congrats! > iirc flet, labels, case, ecase predate Common Lisp and were present > in ancient Emacs Lisp too (but I am not a Lisp historian). Moving > to lexical scoping and at the same time incompatibly renaming those > fundamental lisp forms seems silly to me. I don't know lexical, but what I know is that I have no problems with lexical, and those global packages still work. I have one of them, I keep it global, and I am fine with it all. > They do not even have keyword arglist. Anytime I process complex > data recursively, I reach for labels. There does not seem to be an > alternative, does it? I have no idea about it, let me see what AI says: In **Common Lisp**, `labels` is a powerful macro used for defining **local functions**, including those that are **mutually recursive**. This is particularly useful when working with **recursive algorithms** or **complex data processing**, as it allows you to encapsulate helper functions within the scope of another function without polluting the global namespace. ### **Common Lisp `labels`** #### **Usage Example:** ```lisp (defun compute-factorials (numbers) (labels ((factorial (n) (if (<= n 1) 1 (* n (factorial (1- n)))))) (mapcar #'factorial numbers))) ``` In this example: - **`labels`** defines a local function `factorial` within `compute-factorials`. - The `factorial` function can call itself recursively. - This setup keeps the `factorial` function **encapsulated**, preventing it from being accessible outside `compute-factorials`. ### **Emacs Lisp and `labels`** While **Emacs Lisp** does not natively support `labels` as Common Lisp does, it provides similar functionality through the **Common Lisp extensions** available in Emacs via the `cl-lib` package. Specifically, you can use `cl-labels` to achieve the same effect. #### **Using `cl-labels` in Emacs Lisp:** 1. **Enable `cl-lib`:** Make sure to require the `cl-lib` package at the beginning of your Emacs Lisp file or session: ```elisp (require 'cl-lib) ``` 2. **Define Local Functions with `cl-labels`:** ```elisp (cl-labels ((factorial (n) (if (<= n 1) 1 (* n (factorial (1- n)))))) (mapcar #'factorial '(1 2 3 4 5))) ``` In this example: - **`cl-labels`** is used to define a local `factorial` function within the scope. - The `factorial` function can recursively call itself. - The local function remains **private** to the `cl-labels` block. ### **Alternatives to `labels`** If you prefer not to use `cl-labels`, there are alternative approaches to handle recursion and complex data processing in both Common Lisp and Emacs Lisp: 1. **Global Function Definitions:** Define helper functions globally. This is straightforward but can lead to namespace pollution. ```lisp (defun global-factorial (n) (if (<= n 1) 1 (* n (global-factorial (1- n))))) (defun compute-factorials (numbers) (mapcar #'global-factorial numbers)) ``` 2. **Anonymous Functions and Closures:** Use lambda expressions and closures to encapsulate functionality. ```lisp (defun compute-factorials (numbers) (let ((factorial (lambda (n) (if (<= n 1) 1 (* n (funcall factorial (1- n))))))) (mapcar factorial numbers))) ``` *Note: In Emacs Lisp, this approach can be more cumbersome due to the way closures handle recursion.* 3. **Higher-Order Functions:** Utilize higher-order functions to pass recursive behavior as arguments. ```lisp (defun compute-factorials (numbers) (mapcar (lambda (n) (labels ((factorial (n) (if (<= n 1) 1 (* n (factorial (1- n)))))) (factorial n))) numbers)) ``` ### **Conclusion** - **`labels` in Common Lisp**: Essential for defining local, potentially recursive functions within a function's scope. - **`cl-labels` in Emacs Lisp**: Provides similar functionality through the `cl-lib` package, allowing local and recursive function definitions. - **Alternatives**: While global definitions and other methods exist, `labels` and `cl-labels` offer a clean and encapsulated way to handle recursion and complex data processing. **Therefore, when dealing with recursive processing in Common Lisp or Emacs Lisp, `labels` (or `cl-labels` in Emacs) remains one of the most effective and idiomatic solutions.** --- So I would say, I would not get frustrated, rather just make an alias for me and use `labels' wherever I wish and want. > That means infecting my code with cl-lib silliness. I understand your feelings, but same could be said when you use any Emacs package, the namespaces are not same as in Common Lisp, so it is to be swallowed. Today is cl- and tomorrow is something else like lc- or rcd- or rms- whatever. > And how infecting Emacs with pcase monstrosity feels liberating > compared to simple case and ecase? I have no single use of `pcase' but I know what it does. I like so much `cond'. -- Jean Louis