unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Tim Meehan <btmeehan@gmail.com>
To: Christopher Baines <mail@cbaines.net>
Cc: guile-user <guile-user@gnu.org>
Subject: Re: Somehow I have got this "thunk" thing wrong.
Date: Fri, 5 Mar 2021 21:40:29 -0600	[thread overview]
Message-ID: <CACgrOxJLE8XKWzPQSMKenHDKwKQyk00K8EMqObd-MOmSVdc19g@mail.gmail.com> (raw)
In-Reply-To: <87k0qlnn40.fsf@cbaines.net>

Yes, that makes sense. The unquote is a neat way to fix that.
One of these days, I will learn my lesson.
Thanks Christopher!

On Fri, Mar 5, 2021 at 6:26 PM Christopher Baines <mail@cbaines.net> wrote:

>
> 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?
>


  reply	other threads:[~2021-03-06  3:40 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 [this message]
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=CACgrOxJLE8XKWzPQSMKenHDKwKQyk00K8EMqObd-MOmSVdc19g@mail.gmail.com \
    --to=btmeehan@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=mail@cbaines.net \
    /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).