From: Olivier Dion via General Guile related discussions <guile-user@gnu.org>
To: Tim Meehan <btmeehan@gmail.com>, guile-user <guile-user@gnu.org>
Subject: Re: Somehow I have got this "thunk" thing wrong.
Date: Fri, 05 Mar 2021 19:31:14 -0500 [thread overview]
Message-ID: <87lfb1yvfx.fsf@clara> (raw)
In-Reply-To: <CACgrOxL5X6fz9Ziohjn4hMSv_w=zUxaREVpetFdQ4xXZaGkEwg@mail.gmail.com>
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:31 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 [this message]
2021-03-06 0:48 ` Aleix Conchillo Flaqué
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=87lfb1yvfx.fsf@clara \
--to=guile-user@gnu.org \
--cc=btmeehan@gmail.com \
--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).