unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Maxime Devos <maximedevos@telenet.be>
To: Matt Wette <matt.wette@gmail.com>,
	guile-user@gnu.org, Damien Mattei <damien.mattei@gmail.com>
Subject: Re: Fwd: new function
Date: Sun, 19 Sep 2021 16:56:13 +0200	[thread overview]
Message-ID: <30c86be1bc1ad711ff2bafdf65eb8993092705cb.camel@telenet.be> (raw)
In-Reply-To: <1f773384-058a-f2e3-69cf-b0e23033ec2d@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1834 bytes --]

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 ???))))))
> > 
> > ,expand (define-or-set! a 0) ; $_ = (define a 0)
> > ,expand (let ((a 0)) (define-or-set! a 0) a) ; $_ = (let ((a 0)) (set! a 0) a)
> > (define-or-set! a 0)
> > ,expand (define-or-set! a 0) ; $_ = (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) ; $_ = (let () (define a 0) (set! a 1))
> > 
> > Greetings,
> > Maxime.
> 
> Does this work for you?
> 
> scheme@(guile-user)> (define a 1)
> scheme@(guile-user)> (define a 2)
> scheme@(guile-user)> a
> $2 = 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 instead 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 variable), due to the
first (define-or-set! a 0), the inner 'define-or-set!' should set! 'a' instead 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 ...

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 260 bytes --]

  reply	other threads:[~2021-09-19 14:56 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CADEOadfF+rR=fW0fS-PkQkDN8EHFB1sJnkeePMJyc23m9shLgA@mail.gmail.com>
2021-09-19  9:18 ` Fwd: new function Damien Mattei
2021-09-19 10:38   ` Maxime Devos
2021-09-19 14:45     ` Matt Wette
2021-09-19 14:56       ` Maxime Devos [this message]
2021-09-21 13:04       ` Damien Mattei
2021-09-21 19:03         ` Maxime Devos
2021-09-22  7:52           ` Damien Mattei
2021-09-22 18:03             ` Maxime Devos
2021-09-22 19:12           ` Taylan Kammer

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=30c86be1bc1ad711ff2bafdf65eb8993092705cb.camel@telenet.be \
    --to=maximedevos@telenet.be \
    --cc=damien.mattei@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=matt.wette@gmail.com \
    /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).