From: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
To: Damien Mattei <damien.mattei@gmail.com>
Cc: guile-user <guile-user@gnu.org>
Subject: Re: escaping from a recursive call
Date: Wed, 9 Nov 2022 18:56:28 +0000 [thread overview]
Message-ID: <6fbea690-e51b-567e-238d-16061ab83e5b@posteo.de> (raw)
In-Reply-To: <CADEOadeiDXwvhci1HoKfdj2Ezq5nvorzAoRNMB181ghoPy2C7w@mail.gmail.com>
Hello Damien,
On 11/9/22 18:18, Damien Mattei 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....)
>
> On Wed, Nov 9, 2022 at 5:52 PM Olivier Dion<olivier.dion@polymtl.ca> wrote:
>
>> On Wed, 09 Nov 2022, Damien Mattei<damien.mattei@gmail.com> wrote:
>>> i need a way to escape not only the current call of a recursive function
>>> (it is already done) but alls:
>>>
>>> example:
>>>
>>> (def (foo n)
>>> (cond ((= n 0) 'end0)
>>> ((= n 7) (return 'end7))
>>> (else (cons n (foo {n - 1})))))
>> Is that what you want?
>> --8<---------------cut here---------------start------------->8---
>> (use-modules
>> (ice-9 control))
>>
>> (define (foo n)
>> (let/ec return
>> (let loop ((n n))
>> (cond
>> ((= n 0) 'end0)
>> ((= n 7) (return 'end7))
>> (else
>> (cons n (loop (1- n))))))))
>>
>> (pk (foo 5))
>> (pk (foo 10))
>> --8<---------------cut here---------------end--------------->8---
>>
>> --
>> Olivier Dion
>> oldiob.dev
Another thing you could do is to pass the continuation to the function you are
calling explicitly. I built a small example, which is not really a real-world
use-case, but hopefully explains the idea:
~~~~
(import (except (rnrs base))
(only (guile)
lambda* λ
display
simple-format))
(define factorial-with-continuation
(lambda* (n
#:optional
;; Explicitly specifying an argument, which is a
;; continuation.
;; By default the continuation cont is merely
;; returning the value given. Identity
;; function. Not doing anything with the result.
(cont (λ (x) x))
;; Using `remainder` only as an example.
(test (λ (num) (= (remainder num 7) 0))))
(let iter ([num° (- n 1)]
[product° n])
(cond
;; Base case of factorial.
[(<= num° 1) product°]
;; Next a case with some condition, which, if true
;; means you want to exit.
[(test num°)
(cont product°)]
;; Otherwise normal iteration.
[else
(iter (- num° 1)
(* product° num°))]))))
(factorial-with-continuation 10)
(factorial-with-continuation 10
;; A continuation, which merely
;; display something, but then
;; returns the value.
(λ (num)
(display (simple-format #f "~a\n" num))
num)
;; Another weird condition.
(λ (num) (= (remainder num 17) 0)))
~~~~
In this case the continuation only displays something, but you could make it do
whatever you want, including using its result inside the function you are
calling, instead of returning it. Also you can define the continuation before
calling the function `factorial-with-continuation`.
This might not be the most convenient way, since you have an additional
argument, but maybe it is a very general way of doing something like that.
Regards,
Zelphir
--
repositories:https://notabug.org/ZelphirKaltstahl
prev parent reply other threads:[~2022-11-09 18:56 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
2022-11-10 13:48 ` Damien Mattei
2022-11-09 18:56 ` Zelphir Kaltstahl [this message]
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=6fbea690-e51b-567e-238d-16061ab83e5b@posteo.de \
--to=zelphirkaltstahl@posteo.de \
--cc=damien.mattei@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).