From: Noah Lavine <noah.b.lavine@gmail.com>
To: David Kastrup <dak@gnu.org>
Cc: guile-devel@gnu.org
Subject: Re: Non-stack-copying call-with-current-continuation?
Date: Thu, 1 Mar 2012 20:01:30 -0500 [thread overview]
Message-ID: <CA+U71=PpNzZZQeHUs_mmtZO359CSzqPmR7Qt_cgi73_RETHTXw@mail.gmail.com> (raw)
In-Reply-To: <87mx7zesuw.fsf@fencepost.gnu.org>
Oh yes, you're right. I'm sorry I missed it.
I believe you can do it hygienically though. With prompts, you can use
(make-prompt-tag) to generate a new, unique tag. With catch and throw,
you could use (gensym) to do the same thing. You first example would
become something like
(define-public (find-child music predicate)
"Find the first node in @var{music} that satisfies @var{predicate}."
(let ((music-found-tag (gensym)))
(catch music-found-tag
(lambda ()
(fold-some-music predicate
(lambda (music . _) (throw music-found-tag music))
#f music))
(lambda (key music) music)))
Does that work?
Noah
On Thu, Mar 1, 2012 at 7:42 PM, David Kastrup <dak@gnu.org> wrote:
> Noah Lavine <noah.b.lavine@gmail.com> writes:
>
>> On Thu, Mar 1, 2012 at 7:00 PM, David Kastrup <dak@gnu.org> wrote:
>>>
>>> Hi,
>>>
>>> I am just meddling around with coding and have come up with the
>>> following:
>>>
>>> (define-public (find-child music predicate)
>>> "Find the first node in @var{music} that satisfies @var{predicate}."
>>> (catch 'music-found
>>> (lambda ()
>>> (fold-some-music predicate
>>> (lambda (music . _) (throw 'music-found music))
>>> #f music))
>>> (lambda (key music) music)))
>>>
>>> Now the problem with that is that it is unhygienic. If fold-some-music
>>> were to use music-found signals, or if the predicate did, things would
>>> be awkward. One would need to work with a uniquely generated symbol.
>>> It turns out that the above can be expressed much clearer and cleaner as
>>>
>>> (define-public (find-child music predicate)
>>> "Find the first node in @var{music} that satisfies @var{predicate}."
>>> (call-with-current-continuation
>>> (lambda (music-found)
>>> (fold-some-music predicate
>>> (lambda (music . _) (music-found music))
>>> #f music))))
>>>
>>> at least if I did not make some thinko here. It is basically the same
>>> code and stack-upwards-only, but hygienic. Nothing can call the
>>> continuation but what is inside. Well, of course fold-some-music could
>>> save the closure calling music-found for later. But it doesn't.
>>>
>>> Is there a way to get a call-with-current-continuation that does not
>>> create a stack copy? It is fine if it fails with an exception if one
>>> still tries calling the continuation after it has already returned.
>>
>> If I understand correctly, you only need to call the continuation
>> once. Is that right?
>>
>> In that case, I think you could use either catch and throw or prompts.
>
> Neither of which are hygienic and also less straightforward. It is not
> like I did not start my posting by giving the catch/throw-based example
> and saying what I disliked about it.
>
> --
> David Kastrup
>
>
next prev parent reply other threads:[~2012-03-02 1:01 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-03-02 0:00 Non-stack-copying call-with-current-continuation? David Kastrup
2012-03-02 0:20 ` Noah Lavine
2012-03-02 0:42 ` David Kastrup
2012-03-02 1:01 ` Noah Lavine [this message]
2012-03-02 1:35 ` David Kastrup
2012-03-02 1:49 ` Noah Lavine
2012-03-02 8:36 ` David Kastrup
2012-03-03 5:03 ` Andreas Rottmann
2012-03-03 5:04 ` Andreas Rottmann
2012-03-03 17:48 ` Andy Wingo
2012-03-04 12:01 ` David Kastrup
2012-03-04 12:15 ` Andy Wingo
2012-03-04 13:59 ` David Kastrup
2012-03-04 18:42 ` Andy Wingo
2012-03-04 18:45 ` Mark H Weaver
2012-03-04 23:13 ` David Kastrup
2012-03-05 0:35 ` Mark H Weaver
2012-03-05 1:44 ` David Kastrup
2012-03-02 1:18 ` Nala Ginrut
2012-03-02 1:25 ` Noah Lavine
2012-03-03 17:41 ` Andy Wingo
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='CA+U71=PpNzZZQeHUs_mmtZO359CSzqPmR7Qt_cgi73_RETHTXw@mail.gmail.com' \
--to=noah.b.lavine@gmail.com \
--cc=dak@gnu.org \
--cc=guile-devel@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).