From: "Aleix Conchillo Flaqué" <aconchillo@gmail.com>
To: Olivier Dion <olivier.dion@polymtl.ca>
Cc: guile-user <guile-user@gnu.org>
Subject: Re: Somehow I have got this "thunk" thing wrong.
Date: Fri, 5 Mar 2021 16:48:12 -0800 [thread overview]
Message-ID: <CA+XASoVeNfYB+tJ8dF+edinoy0NCMnHLkNMR1fnZop+Y_QaSwg@mail.gmail.com> (raw)
In-Reply-To: <87lfb1yvfx.fsf@clara>
Or you can always use eval.
((eval res (interaction-environment)))
Sorry... I was watching "The Most Beautiful Program Ever Written" talk
from W. Byrd and got too excited :-D
Aleix
On Fri, Mar 5, 2021 at 4:30 PM Olivier Dion via General Guile related
discussions <guile-user@gnu.org> wrote:
>
> On Fri, 05 Mar 2021, Tim Meehan <btmeehan@gmail.com> wrote:
> > I wanted to store a thunk in a hashtable so that I could look up its key
> > and then run it later. Something like this:
> >
> > #! /usr/bin/guile
> > !#
> >
> > (use-modules (ice-9 hash-table))
> >
> > (define stuff (alist->hash-table
> > '((a . (lambda () (display "event a\n")))
> > (b . (lambda () (display "event b\n")))
> > (c . (lambda () (display "event c\n"))))))
>
> You've quoted the whole s-exp. Which means lambda is never applied.
> You have to apply lambda in order to create a procedure.
>
> >
> > (define res (hash-ref stuff 'a))
> > (res)
> >
> > But when I run it:
> > Wrong type to apply: (lambda () (display "event a\n"))
>
> You're trying to apply a list and not a procedure. This is because of
> the quote mentioned above. In other words, you're doing this:
> ----------------------------------------------------------------------
> (apply '(lambda () (display "event a\n")))
> ----------------------------------------------------------------------
>
> Change the ' for ` and unquote the lambdas with ,. Like this:
> ----------------------------------------------------------------------
> (define stuff (alist->hash-table
> `((a . ,(lambda () (display "event a\n")))
> (b . ,(lambda () (display "event b\n")))
> (c . ,(lambda () (display "event c\n"))))))
> ----------------------------------------------------------------------
>
> Or use the list procedure instead of quoting:
> ----------------------------------------------------------------------
> #! /usr/bin/guile
> !#
>
> (use-modules (ice-9 hash-table))
>
> (define stuff (alist->hash-table
> (list (cons 'a (lambda () (display "event a\n")))
> (cons 'b (lambda () (display "event b\n")))
> (cons 'c (lambda () (display "event c\n"))))))
>
> (define res (hash-ref stuff 'a))
> (res)
> ----------------------------------------------------------------------
>
> --
> Olivier Dion
> PolyMtl
>
next prev parent reply other threads:[~2021-03-06 0:48 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-06 0:05 Somehow I have got this "thunk" thing wrong Tim Meehan
2021-03-06 0:26 ` Christopher Baines
2021-03-06 3:40 ` Tim Meehan
2021-03-06 3:43 ` Tim Meehan
2021-03-06 0:31 ` Olivier Dion via General Guile related discussions
2021-03-06 0:48 ` Aleix Conchillo Flaqué [this message]
2021-03-06 16:55 ` Taylan Kammer
2021-03-06 17:16 ` Olivier Dion via General Guile related discussions
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+XASoVeNfYB+tJ8dF+edinoy0NCMnHLkNMR1fnZop+Y_QaSwg@mail.gmail.com \
--to=aconchillo@gmail.com \
--cc=guile-user@gnu.org \
--cc=olivier.dion@polymtl.ca \
/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).