guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Somehow I have got this "thunk" thing wrong.


From: Tim Meehan
Subject: Re: Somehow I have got this "thunk" thing wrong.
Date: Fri, 5 Mar 2021 21:40:29 -0600

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 via email to

[Prev in Thread] Current Thread [Next in Thread]