all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: arthur miller <arthur.miller@live.com>
To: Yuri Khan <yuri.v.khan@gmail.com>
Cc: Eli Zaretskii <eliz@gnu.org>, "ams@gnu.org" <ams@gnu.org>,
	"emacs-devel@gnu.org" <emacs-devel@gnu.org>
Subject: Sv: Is this a bug in while-let or do I missunderstand it?
Date: Mon, 11 Nov 2024 08:49:39 +0000	[thread overview]
Message-ID: <DU2PR02MB10109B8D15A32AE84FDEF507F96582@DU2PR02MB10109.eurprd02.prod.outlook.com> (raw)
In-Reply-To: <CAP_d_8VHa13UtofAXS55WzCm8pCJs1tzuFofKjNTwu+yjKX6DQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3044 bytes --]

>Comprarison with a for loop is somewhat strained here. The while-let

I didn't meant to say that while-let was equivalent to that for-loop;
but tried to illustrate the expectaions. I hope it was clear from the rest.

>loop in Elisp is directly analogous to this C++ while loop:
>
>    #include <iostream>
>
>    int main() {
>        while (bool run = true) {
>            std::cout << "running\n";
>            run = false;
>        }
>        std::cout << "out of loop\n";
>    }
>
>and yes, it’s an infloop, too.

Actually didn't know we can introduce new variable in while declaration in
C++; in C it is verbotten:

~/repos/test $ gcc -o test test.c
test.c: In function 'main':
test.c:3:10: error: expected expression before 'int'
    3 |   while (int i = 0) {
      |          ^~~
test.c:4:5: error: 'i' undeclared (first use in this function)
    4 |     i++;
      |     ^
test.c:4:5: note: each undeclared identifier is reported only once for each function it appears in

But that is just a regression (thought it as in C++ too :-)).

>What you’re looking for, though, seems to be a while loop with a
>break, which is expressed as a catch/throw in Elisp.

Yes, that is what I came to as well, if you check the rest of the
response to Eli as I suggested to mention catch/throw or cl-block/cl-return-from
in the docs.

Even better is named-let, which seems to be a general
version of while-let:

(defmacro while-test (spec &rest body)
  (declare (indent defun))
  (let* ((name (gensym "while-let-"))
         (bindings (if (and (consp spec) (symbolp (car spec)))
                       (list spec)
                     spec)))
    `(named-let ,name ,spec
       ,@body
       (if (not (and ,@(mapcar #'car bindings)))
           nil
         (,name ,@(mapcar #'cadr bindings))))))

(pp (macroexpand-1
     '(while-test ((run t))
        (setf run nil))) (current-buffer))

(named-let while-let-141 ((run t))
  (setf run nil) (if (not (and run)) nil (while-let-141 t)))

(pp (macroexpand-1
     '(while-let ((run t))
        (setf run nil))) (current-buffer))

(catch 'done140
  (while t (if-let* ((run t)) (progn (setf run nil)) (throw 'done140 nil))))

As seen, they both expand to equivalent infinite loop.

For the illustration, named-let expands to a nice while loop itself:

(pp (macroexpand-all
     '(while-test ((run t))
        (setf run nil))) (current-buffer))

(let ((run t))
  (let (retval)
    (while
        (let ((run run))
          (progn
            (setq run nil)
            (if (not (and run)) nil (progn (setq run t) :recurse)))))
    retval))

In my personal opinion while-let, while meant to be a "shortcut" to
certain style of expressions is a bit unfortunate name, since the "-let"
part of the name suggest establishing an environment around the body,
however that environment is read only which is not normal semantic of
let-bindings.  In other words, the devil is in the details which perhaps was
 not intentional?

[-- Attachment #2: Type: text/html, Size: 14124 bytes --]

  reply	other threads:[~2024-11-11  8:49 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-08 16:25 Is this a bug in while-let or do I missunderstand it? arthur miller
2024-11-08 19:23 ` Philip Kaludercic
2024-11-09  3:30   ` Sv: " arthur miller
2024-11-09  9:29 ` Yuri Khan
2024-11-09 13:03   ` Sv: " arthur miller
2024-11-09 13:15     ` Yuri Khan
2024-11-09 13:38       ` Sv: " arthur miller
2024-11-09 13:41         ` Yuri Khan
2024-11-09 13:47           ` Sv: " arthur miller
2024-11-09 14:04             ` Yuri Khan
2024-11-09 14:44               ` Sv: " arthur miller
2024-11-09 16:33               ` Alfred M. Szmidt
2024-11-09 16:44                 ` Eli Zaretskii
2024-11-09 16:53                   ` Eli Zaretskii
2024-11-09 17:33                   ` Andreas Schwab
2024-11-09 18:07                   ` [External] : " Drew Adams
2024-11-09 18:18                     ` Alfred M. Szmidt
2024-11-09 20:02                       ` Jens Schmidt
2024-11-09 20:38                         ` Alfred M. Szmidt
2024-11-09 21:18                           ` Joost Kremers
2024-11-10 11:44                         ` Alfred M. Szmidt
2024-11-10 12:24                           ` Better documentation for non-binding clauses of if-let and friends Jens Schmidt
2024-11-10 14:51                             ` Sean Whitton
2024-11-10 16:58                               ` Jens Schmidt
2024-11-11 10:03                               ` Alfred M. Szmidt
2024-11-11  8:20                             ` Alfred M. Szmidt
2024-11-09 19:32                     ` Sv: [External] : Re: Is this a bug in while-let or do I missunderstand it? arthur miller
2024-11-09 22:36                       ` Drew Adams
2024-11-09 22:53                         ` Drew Adams
2024-11-14 21:50                   ` John ff
2024-11-09 20:29                 ` Sv: " arthur miller
2024-11-10  6:22                   ` Eli Zaretskii
2024-11-10 10:40                     ` Joost Kremers
2024-11-10 12:10                       ` Alfred M. Szmidt
2024-11-10 19:49                         ` Sv: " arthur miller
2024-11-10 18:18                     ` arthur miller
2024-11-11  5:13                       ` Yuri Khan
2024-11-11  8:49                         ` arthur miller [this message]
2024-11-11 12:23                           ` Sv: " tomas
2024-11-11 22:41                     ` Joost Kremers
2024-11-12 12:19                       ` Eli Zaretskii
2024-11-12 12:45                         ` Joost Kremers
2024-11-12 14:34                           ` Eli Zaretskii
2024-11-12 15:32                             ` Joost Kremers
2024-11-12 23:45                         ` Joost Kremers
2024-11-13  9:45                           ` Sean Whitton
2024-11-13  9:56                             ` Sean Whitton
2024-11-13 11:00                               ` Joost Kremers
2024-11-13 12:17                                 ` Sean Whitton
2024-11-14  7:55                                 ` Eli Zaretskii
2024-11-14  8:21                                   ` Joost Kremers
2024-11-14 21:51                               ` John ff
2024-11-14 21:52                                 ` John ff
2024-11-09 21:47             ` Sv: " Joost Kremers
2024-11-09 22:07               ` Sv: " arthur miller
2024-11-10  6:07               ` Andreas Schwab
  -- strict thread matches above, loose matches on Subject: below --
2024-11-12  3:36 arthur miller
2024-11-12  8:30 ` Joost Kremers
2024-11-12 17:55   ` Alfred M. Szmidt
2024-11-12 23:21     ` Sv: " arthur miller
2024-11-12 23:31       ` Joost Kremers
2024-11-12 23:08   ` arthur miller

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=DU2PR02MB10109B8D15A32AE84FDEF507F96582@DU2PR02MB10109.eurprd02.prod.outlook.com \
    --to=arthur.miller@live.com \
    --cc=ams@gnu.org \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=yuri.v.khan@gmail.com \
    /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.