From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Maxime Devos Newsgroups: gmane.lisp.guile.user Subject: Re: Fwd: new function Date: Sun, 19 Sep 2021 16:56:13 +0200 Message-ID: <30c86be1bc1ad711ff2bafdf65eb8993092705cb.camel@telenet.be> References: <1f773384-058a-f2e3-69cf-b0e23033ec2d@gmail.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg="pgp-sha512"; protocol="application/pgp-signature"; boundary="=-kQJ/pHdZGwR9xx4XzPiT" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34005"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Evolution 3.34.2 To: Matt Wette , guile-user@gnu.org, Damien Mattei Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Sep 19 16:57:02 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 1mRyFa-0008eH-CH for guile-user@m.gmane-mx.org; Sun, 19 Sep 2021 16:57:02 +0200 Original-Received: from localhost ([::1]:48858 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mRyFZ-0002Zl-0F for guile-user@m.gmane-mx.org; Sun, 19 Sep 2021 10:57:01 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58690) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mRyEz-0002Ye-N0 for guile-user@gnu.org; Sun, 19 Sep 2021 10:56:26 -0400 Original-Received: from laurent.telenet-ops.be ([2a02:1800:110:4::f00:19]:38956) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mRyEx-0001FY-FS for guile-user@gnu.org; Sun, 19 Sep 2021 10:56:25 -0400 Original-Received: from ptr-bvsjgyjmffd7q9timvx.18120a2.ip6.access.telenet.be ([IPv6:2a02:1811:8c09:9d00:aaf1:9810:a0b8:a55d]) by laurent.telenet-ops.be with bizsmtp id vqwH2500A0mfAB401qwJxm; Sun, 19 Sep 2021 16:56:18 +0200 In-Reply-To: <1f773384-058a-f2e3-69cf-b0e23033ec2d@gmail.com> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telenet.be; s=r21; t=1632063378; bh=aXiD2iTPcRDnY9UHXuyascnW01K4p5PbDVB7yDsO7ls=; h=Subject:From:To:Date:In-Reply-To:References; b=JvlqX1lqmxTXEBL9B0kE1O3jXLU94LfvROfVslj/xQVjqrzpLsHkXJfgdP3DxxGSM T4oEJWW5vvoN3dfD1Y32/kPkPn7LDQMVd+8+JjAP+CeXFQcF5z+faiul1Vknwz9n1C kN2GAd0gEirNxVCS68nRBTBQjVwFSmANbzY5aRa7CqmcOTBQfrj+k1x6wK49ayDP3u wcNELYuEL0nmj/e6wrcN9LH7MbHNSNjfWN3qoEZ0o5YN0uwyDJQLTWe/bbwSX8uFHE Bmj07fzsDOj1eckyIeGRM9BB88tDwnFbXiHiXSw9H63+46UBc9Q/3ZYT4GVcF7QEqs UIwB+nMEaUVKA== Received-SPF: pass client-ip=2a02:1800:110:4::f00:19; envelope-from=maximedevos@telenet.be; helo=laurent.telenet-ops.be X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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_LOW=-0.7, 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:17759 Archived-At: --=-kQJ/pHdZGwR9xx4XzPiT Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Matt Wette schreef op zo 19-09-2021 om 07:45 [-0700]: > On 9/19/21 3:38 AM, Maxime Devos wrote: > > [...] > > (use-modules (system syntax)) > > (define-syntax define-or-set! > > (lambda (s) > > (syntax-case s () > > ((_ var value) > > (case (syntax-local-binding #'var) > > ((lexical displaced-lexical) #'(set! var value)) > > ((global) #'(define var value)) > > (else ???)))))) > >=20 > > ,expand (define-or-set! a 0) ; $_ =3D (define a 0) > > ,expand (let ((a 0)) (define-or-set! a 0) a) ; $_ =3D (let ((a 0)) (set= ! a 0) a) > > (define-or-set! a 0) > > ,expand (define-or-set! a 0) ; $_ =3D (define a 0) (might or might not = be acceptable for your use case > > ,expand (let () (define-or-set! a 0) (define-or-set! a 1) a) ; $_ =3D (= let () (define a 0) (set! a 1)) > >=20 > > Greetings, > > Maxime. >=20 > Does this work for you? >=20 > scheme@(guile-user)> (define a 1) > scheme@(guile-user)> (define a 2) > scheme@(guile-user)> a > $2 =3D 2 > scheme@(guile-user)> If I use 'define', the following evaluates to zero: (define a 0) (let () (define a 1) (values)) a (the inner 'a' shadows the global 'a', thus the last a evaluates to 0 inste= ad of 1) However, Damien Mattei asked for a variant of 'define' that only defines a = variable if it is not yet bound (let's call this variant define-or-set!) (define-or-set! a 0) (let () (define-or-set! a 1) (values)) a As within the 'let' expression, 'a' is already bound (to the global variabl= e), due to the first (define-or-set! a 0), the inner 'define-or-set!' should set! 'a' inst= ead of defining a new variable 'a'. As such, the code above should expand to: (define a 0) (let () (set! a 1) (values)) a ; evaluates to 1. Possibly Damien Mattei wants something else though ... --=-kQJ/pHdZGwR9xx4XzPiT Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- iI0EABYKADUWIQTB8z7iDFKP233XAR9J4+4iGRcl7gUCYUdPjRccbWF4aW1lZGV2 b3NAdGVsZW5ldC5iZQAKCRBJ4+4iGRcl7itBAQCs75KQvhEb34EaoF7BB7BOeuAu Hc9i2mz0exHnNmpjSAD/dDmxdfh7q46NuVeGj/QJNahgb8CAlmUQflTEorOcYgA= =Nx8z -----END PGP SIGNATURE----- --=-kQJ/pHdZGwR9xx4XzPiT--