* Somehow I have got this "thunk" thing wrong. @ 2021-03-06 0:05 Tim Meehan 2021-03-06 0:26 ` Christopher Baines 2021-03-06 0:31 ` Olivier Dion via General Guile related discussions 0 siblings, 2 replies; 8+ messages in thread From: Tim Meehan @ 2021-03-06 0:05 UTC (permalink / raw) To: guile-user 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")) ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Somehow I have got this "thunk" thing wrong. 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 0:31 ` Olivier Dion via General Guile related discussions 1 sibling, 1 reply; 8+ messages in thread From: Christopher Baines @ 2021-03-06 0:26 UTC (permalink / raw) To: Tim Meehan; +Cc: guile-user [-- 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 --] ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Somehow I have got this "thunk" thing wrong. 2021-03-06 0:26 ` Christopher Baines @ 2021-03-06 3:40 ` Tim Meehan 2021-03-06 3:43 ` Tim Meehan 0 siblings, 1 reply; 8+ messages in thread From: Tim Meehan @ 2021-03-06 3:40 UTC (permalink / raw) To: Christopher Baines; +Cc: guile-user 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? > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Somehow I have got this "thunk" thing wrong. 2021-03-06 3:40 ` Tim Meehan @ 2021-03-06 3:43 ` Tim Meehan 0 siblings, 0 replies; 8+ messages in thread From: Tim Meehan @ 2021-03-06 3:43 UTC (permalink / raw) To: Christopher Baines; +Cc: guile-user Also thanks Olivier and Aleix, and now I will have to watch that talk as well. On Fri, Mar 5, 2021 at 9:40 PM Tim Meehan <btmeehan@gmail.com> wrote: > 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? >> > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Somehow I have got this "thunk" thing wrong. 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 0:31 ` Olivier Dion via General Guile related discussions 2021-03-06 0:48 ` Aleix Conchillo Flaqué 2021-03-06 16:55 ` Taylan Kammer 1 sibling, 2 replies; 8+ messages in thread From: Olivier Dion via General Guile related discussions @ 2021-03-06 0:31 UTC (permalink / raw) To: Tim Meehan, guile-user 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 ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Somehow I have got this "thunk" thing wrong. 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 1 sibling, 0 replies; 8+ messages in thread From: Aleix Conchillo Flaqué @ 2021-03-06 0:48 UTC (permalink / raw) To: Olivier Dion; +Cc: guile-user 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 > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Somehow I have got this "thunk" thing wrong. 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 1 sibling, 1 reply; 8+ messages in thread From: Taylan Kammer @ 2021-03-06 16:55 UTC (permalink / raw) To: Olivier Dion, Tim Meehan, guile-user On 06.03.2021 01:31, Olivier Dion via General Guile related discussions wrote: > You've quoted the whole s-exp. Which means lambda is never applied. > You have to apply lambda in order to create a procedure. Just a bit of pedantry on the terms: 'lambda' is not "applied" anyway, because it's a special form and not a procedure. The term "apply" is used when referring to procedures. E.g. you would apply 'list', 'cons', 'vector-ref' and so on, but you wouldn't apply 'lambda', 'if', 'define' and so on. In other words, if you can pass it as an argument to 'apply', you can "apply" it. E.g. '(apply lambda (list ...))' is invalid syntax. I think the term "evaluate" would be preferred here, as in: "the 'lambda' is quoted so it won't be evaluated." - Taylan ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Somehow I have got this "thunk" thing wrong. 2021-03-06 16:55 ` Taylan Kammer @ 2021-03-06 17:16 ` Olivier Dion via General Guile related discussions 0 siblings, 0 replies; 8+ messages in thread From: Olivier Dion via General Guile related discussions @ 2021-03-06 17:16 UTC (permalink / raw) To: Taylan Kammer, Tim Meehan, guile-user On Sat, 06 Mar 2021, Taylan Kammer <taylan.kammer@gmail.com> wrote: > On 06.03.2021 01:31, Olivier Dion via General Guile related discussions > wrote: >> You've quoted the whole s-exp. Which means lambda is never applied. >> You have to apply lambda in order to create a procedure. > > Just a bit of pedantry on the terms: 'lambda' is not "applied" anyway, > because it's a special form and not a procedure. > > The term "apply" is used when referring to procedures. E.g. you would > apply 'list', 'cons', 'vector-ref' and so on, but you wouldn't apply > 'lambda', 'if', 'define' and so on. > > In other words, if you can pass it as an argument to 'apply', you can > "apply" it. E.g. '(apply lambda (list ...))' is invalid syntax. > > I think the term "evaluate" would be preferred here, as in: "the > 'lambda' is quoted so it won't be evaluated." You're totally right! Lambda is evaluated to a closure. The latter is then applied to evaluate its expressions in the captured environment. The following video explains the metacircular evaluator in great details! Highly recommend all other lectures as well :-) <https://www.youtube.com/watch?v=aAlR3cezPJg&t=743s> > > - Taylan -- Olivier Dion PolyMtl ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2021-03-06 17:16 UTC | newest] Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 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é 2021-03-06 16:55 ` Taylan Kammer 2021-03-06 17:16 ` Olivier Dion via General Guile related discussions
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).