unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Christopher Baines <mail@cbaines.net>
To: Tim Meehan <btmeehan@gmail.com>
Cc: guile-user@gnu.org
Subject: Re: Somehow I have got this "thunk" thing wrong.
Date: Sat, 06 Mar 2021 00:26:39 +0000	[thread overview]
Message-ID: <87k0qlnn40.fsf@cbaines.net> (raw)
In-Reply-To: <CACgrOxL5X6fz9Ziohjn4hMSv_w=zUxaREVpetFdQ4xXZaGkEwg@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1058 bytes --]


Tim Meehan <btmeehan@gmail.com> writes:

> 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"))))))
>
> (define res (hash-ref stuff 'a))
> (res)
>
> But when I run it:
> Wrong type to apply: (lambda () (display "event a\n"))

The lambda bit you've written is quoted. So you're asking Guile to apply
a list where the first element is the symbol 'lambda, the second is the
empty list, ...

You probably want something like this, where you're creating a list of
pairs, where the car of the pair is a symbol, and the cdr is a procedure
(rather than a list).

 (define stuff (alist->hash-table
   `((a . ,(lambda () (display "event a\n")))
     (b . ,(lambda () (display "event b\n")))
     (c . ,(lambda () (display "event c\n"))))))

Does that make sense?

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 987 bytes --]

  reply	other threads:[~2021-03-06  0:26 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 [this message]
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é
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=87k0qlnn40.fsf@cbaines.net \
    --to=mail@cbaines.net \
    --cc=btmeehan@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).