From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: "Garreau\, Alexandre" Newsgroups: gmane.emacs.devel Subject: Re: dolist considered harmful Date: Wed, 31 Oct 2018 23:02:57 +0100 Message-ID: <877ehxsrji.fsf@portable.galex-713.eu> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1541023771 14179 195.159.176.226 (31 Oct 2018 22:09:31 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 31 Oct 2018 22:09:31 +0000 (UTC) User-Agent: Gnus (5.13), GNU Emacs 25.1.1 (i686-pc-linux-gnu, GTK+ Version 3.22.11) of 2017-09-15, modified by Debian Cc: emacs-devel@gnu.org To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Oct 31 23:09:27 2018 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gHyg2-0003Zy-IG for ged-emacs-devel@m.gmane.org; Wed, 31 Oct 2018 23:09:26 +0100 Original-Received: from localhost ([::1]:33750 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gHyi8-0001oG-UI for ged-emacs-devel@m.gmane.org; Wed, 31 Oct 2018 18:11:36 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59410) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gHyZw-0003dV-DY for emacs-devel@gnu.org; Wed, 31 Oct 2018 18:03:10 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gHyZv-0002s3-CM for emacs-devel@gnu.org; Wed, 31 Oct 2018 18:03:08 -0400 Original-Received: from portable.galex-713.eu ([2a00:5884:8305::1]:46002) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gHyZu-0002px-TC for emacs-devel@gnu.org; Wed, 31 Oct 2018 18:03:07 -0400 Original-Received: from localhost ([::1] helo=portable.galex-713.eu) by portable.galex-713.eu with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gHyZl-0003Wz-KI; Wed, 31 Oct 2018 23:02:57 +0100 PGP-FINGERPRINT: E109 9988 4197 D7CB B0BC 5C23 8DEB 24BA 867D 3F7F Accept-Language: fr, en, eo, it, br In-Reply-To: (Stefan Monnier's message of "Tue, 30 Oct 2018 14:39:46 -0400") X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:5884:8305::1 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:230920 Archived-At: On 2018-10-30 at 14:39, Stefan Monnier wrote: > All this pcase-dolist discussion made me take a second look at dolist > and now I wondering whether we should keep it: This is still extreme: I never saw anyone suggesting to remove anything about pcase or its derivative=E2=80=A6 but I can get quite distracted somet= imes so maybe I missed it. I think pattern-matching, though I=E2=80=99m not fon= d of it, is sometimes necessary, and is a must-be in any high-level language, and as pcase exists and is pretty used=E2=80=A6 > Since it can only be used when we're 100% sure that LIST is a LIST, > (and who can be sure of such a thing in a dynamic language like Emacs > Lisp), I think we'd be better off deprecating it and changing all uses > of dolist with a clear while+cdr loop that everyone can understand. Before people spoke about sarcasm I really thought this all was serious and first degree, about removing a such now idiomatic form from elisp, that also exists in common lisp, to end suggesting such much lower level style. Also I dislike `while': it looks pretty not lispy to me, very imperative (but well all emacs and much lisp is imperative as well=E2=80=A6)=E2=80=A6 = I=E2=80=99d rather define `dolist' upon `mapc' than `while'. And if something common and simple as `dolist' was to be removed, I think I=E2=80=99d end up programming like in scheme and use recursion everywhere (btw is emacs tail-recursive?), so in the end that=E2=80=99d divide programming style bet= ween `while' and recursion so all programs becomes half as readable as before (except recursion-people are afaik more rare, so I=E2=80=99m going to be fo= rced in `while', and anyway it seems like in the end I made an argument in favor of `pcase' and against `case' ><). > - The docstring is incomprehensible: > > Loop over a list. > Evaluate BODY with VAR bound to each car from LIST, in turn. > Then evaluate RESULT to get return value, default nil. > > I don't know how to loop over a list. I must say, that this seems true to me. I regularely prefers recursion or mapcar/mapc to dolist because they=E2=80=99re simpler and cleaner. I re= call at a time, each time I realized I needed `dolist', I evaled a test using it so to be sure I still knew how to properly use it. Still today, before writing it, it is unclear in my mind how I would use dolist to do something (I have to say I find unnatural to repetedly use setq or use side-effects (especially to set stuff) to achieve something). Last time I used `dolist', I first wanted to use recursion before someone noticed me it was unnatural / strang / unidiomatic in elisp to do that, so I needed dolist (or seq, which still seems more uncommon to me), while what I wanted to do was simply to `reduce' composition (I got a list of functions, I wanted to apply them all to a string, one upon the other: so if there were a `compose' function, `reduce' it to a sequence of functions upon a string). > Did they mean to set the cdr of the last element so it loops back to > the first element? I have to say that, though it is closer to real, low-level, actual meaning/behavior, and not difficult to grasp once you know what=E2=80=99s a list, =E2=80=9Cto each element of LIST, in turn=E2=80=9D would have been mo= re simple to read, parse (it is a syntactically simpler phrase), understand, including for, but not limited to, newcomers. > "evaluate" is unclear (is it passed to `eval`? Then why not say it? > What second arg is passed to `eval`?). but why evaluate it several > times? Also, in which order? How does RESULT communicate the > return value? Is it RESULT which defaults to nil or the return > value? If the return value, then when does it default to nil? Again I have to say =E2=80=9CThen return RESULT evaluation, or nil if omitt= ed.=E2=80=9D is simpler and more unambiguous. > - The docstring says nothing of what happens when LIST is not a list, > yet a quick grep shows that most uses of dolist use for LIST a simple > variable rather than a list. What gives? a variable=E2=80=A6 returning a list, so being a list (the symbol is not a = list, but it doesn=E2=80=99t say it doesn=E2=80=99t evaluate the variable). Again I have to say (this formulation is getting a little too much redundant here and adding =E2=80=9Cagain=E2=80=9D is not hiding it well=E2= =80=A6) I find it is true that `dolist' docstring is not explicit enough about what is quoted, to quote, evaluated or not. It no longer happens to me, but I clearly recall having some pain about `dolist' in my first times I used it, to understand what I needed to quote and what I needed to put as is=E2= =80=A6 and that required me several empirical tests to be sure: ideally a clear docstring should spare you that. Because there=E2=80=99s some inconstancy to explain there: VAR is not evalu= ated, never, it *must* be a symbol as `read' would read it, not something evaluating to a symbol, LIST is evaluated right away, and RESULT is too, only once, and at the end.