From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?Aleix_Conchillo_Flaqu=C3=A9?= Newsgroups: gmane.lisp.guile.user Subject: Re: Somehow I have got this "thunk" thing wrong. Date: Fri, 5 Mar 2021 16:48:12 -0800 Message-ID: References: <87lfb1yvfx.fsf@clara> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="13618"; mail-complaints-to="usenet@ciao.gmane.io" Cc: guile-user To: Olivier Dion Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sat Mar 06 01:48:49 2021 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lIL7h-0003RS-4d for guile-user@m.gmane-mx.org; Sat, 06 Mar 2021 01:48:49 +0100 Original-Received: from localhost ([::1]:55702 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lIL7g-0005ZN-7d for guile-user@m.gmane-mx.org; Fri, 05 Mar 2021 19:48:48 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:44794) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lIL7L-0005Z2-9z for guile-user@gnu.org; Fri, 05 Mar 2021 19:48:27 -0500 Original-Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]:37235) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lIL7J-0003XS-ID for guile-user@gnu.org; Fri, 05 Mar 2021 19:48:26 -0500 Original-Received: by mail-pl1-x62c.google.com with SMTP id p5so2270069plo.4 for ; Fri, 05 Mar 2021 16:48:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=ftHTNX9/Y6txNBMY8dv0QV3gpi6JLePxRKA6aVsFqx4=; b=Ecutmcp4C0IBI4Pq3oDvDFMTae2yz6mr3FcUwbfi+1BVBLEpRrVDnsNvZhMo/Jzel9 R9jtCRNRtLE9qyZZYv0EL7OwD+V4uVQM5vjlbzffEb3PDHYHH8JD/wg/Xn9M0Xbo+Jcr GQEWPITPGXNRvalfgIIvePrPAo6cKXUkdTnFt0G92+5P33UM8EaDSN/dUKuxNnRIuMPD 8zV0qU5/XNt7FNPRSXpT6gmqmyR0ZUeKEWhg/QEzyUB24t/Y62GSMSU7DCSbSMvF3FGg SxO/rPKtsT/vnkoqd5GE7Z/oVPtVjS7RUABfNQUsthN59Oa6wb2kK0Y1XH6n3lX+AWYy lBDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ftHTNX9/Y6txNBMY8dv0QV3gpi6JLePxRKA6aVsFqx4=; b=YB2mA1oABGKp8q/4RD7B8F5G0lf1Jq5OnkMihoXvFShy8tzt7rZ9MsI/PCBD4Xuai9 SNbdB5fKJKXV5QoziKXDJV6Jsq9gwBVKLRAstn5kPLuSWzSuTO6DXnSlqFyoJAEk0j6H J7FPPVLH3rhWJHa3+Bb9oAxE5pTN5tPzG2aQps3X/i2QyB3InEuW2qokSimR1XvqpZhw 9n7aQ+PViAi8oSCL4wAC97Vfbyqk0Nl/mj23gFj7d7fyurDpHEiipgfdo+ub1wrJOCNW Z+3zTsnR8l1U+j5qUk4iRxv8vU0S9WYKVO9/Bhy9VLyVODlVM50eF6fuxMGnhj34h4fG t4Uw== X-Gm-Message-State: AOAM5322sivvu2A0QWmJBH9WfIqUVXTZ/wBt5WupotkeEX0BZz77OkXR OKYmA8Y8/sAvmPbmasJp1o8vciYrVJMDAoHVQ6s= X-Google-Smtp-Source: ABdhPJzXM4Fu2zq7BZNtrVvEmhMv6b53JzsvLmL5anaayolyQNnKvGsgGpS2qdlZIfFZiALWFP370RYQvqADbOzisX0= X-Received: by 2002:a17:902:a9c5:b029:e3:f905:51f6 with SMTP id b5-20020a170902a9c5b02900e3f90551f6mr10706697plr.15.1614991703418; Fri, 05 Mar 2021 16:48:23 -0800 (PST) In-Reply-To: <87lfb1yvfx.fsf@clara> Received-SPF: pass client-ip=2607:f8b0:4864:20::62c; envelope-from=aconchillo@gmail.com; helo=mail-pl1-x62c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17339 Archived-At: 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 wrote: > > On Fri, 05 Mar 2021, Tim Meehan 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 >