From: Chris Vine <vine.chris@gmail.com>
To: guile-user@gnu.org
Subject: Re: escaping from a recursive call
Date: Thu, 10 Nov 2022 12:32:44 +0000 [thread overview]
Message-ID: <20221110123244.af0bdfe7bb0ed55f9b761dff@gmail.com> (raw)
In-Reply-To: <87y1sknhb5.fsf@laura>
On Wed, 09 Nov 2022 12:55:42 -0500
Olivier Dion via General Guile related discussions <guile-user@gnu.org>
wrote:
> On Wed, 09 Nov 2022, Damien Mattei <damien.mattei@gmail.com> wrote:
> > but in the general case , i want a macro that can do it on any function
> > (i'm not sure it can be done because the continuation have to be captured
> > just before the call to the function and be inlined at the good
> > place....)
>
> I'm not aware of any control mechanism that are implicit in Guile. You
> almost always have to deal with a continuation object. However, nothing
> prevent you to invent your own control flow wrapper.
You can construct an anaphoric macro with that in mind. This introduces
an imperative-style 'loop' macro which carries within the loop block a
'break' keyword which will cause the loop to exit:
(use-modules (ice-9 control)) ;; for call/ec
(define-syntax loop
(lambda (x)
(syntax-case x ()
[(k e ...)
(with-syntax ([break (datum->syntax #'k 'break)])
#'(call/ec
(lambda (break)
(let f () e ... (f)))))])))
(display (let ([n 3] [lst '()])
(loop
(if (= n 0) (break lst))
(set! lst (cons 'a lst))
(set! n (- n 1)))))
(newline)
However explicit control of loops is better in my view. Imperative
loops usually end up with mutable bindings, as in the example above.
next prev parent reply other threads:[~2022-11-10 12:32 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-09 15:56 escaping from a recursive call Damien Mattei
2022-11-09 16:51 ` Olivier Dion via General Guile related discussions
2022-11-09 17:18 ` Damien Mattei
2022-11-09 17:55 ` Olivier Dion via General Guile related discussions
2022-11-09 18:49 ` Damien Mattei
2022-11-10 1:20 ` Olivier Dion via General Guile related discussions
2022-11-10 4:56 ` tomas
2022-11-10 6:01 ` Damien Mattei
2022-11-10 5:25 ` Damien Mattei
2022-11-10 17:03 ` Olivier Dion via General Guile related discussions
2022-11-10 12:32 ` Chris Vine [this message]
2022-11-10 13:48 ` Damien Mattei
2022-11-09 18:56 ` Zelphir Kaltstahl
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
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221110123244.af0bdfe7bb0ed55f9b761dff@gmail.com \
--to=vine.chris@gmail.com \
--cc=guile-user@gnu.org \
/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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).