From: Damien Mattei <damien.mattei@gmail.com>
To: Jeronimo Pellegrini <j_p@aleph0.info>
Cc: Pierpaolo Bernardi <olopierpa@gmail.com>,
guile-user <guile-user@gnu.org>
Subject: Re: cond clause does not allow definitions
Date: Thu, 23 May 2024 16:25:27 +0200 [thread overview]
Message-ID: <CADEOadfBZeJD=tBJq0gyfdDa=GA6iS3zP23jt1k44hSzKrxrPQ@mail.gmail.com> (raw)
In-Reply-To: <9ff8d2d644823f62e416047582433d2b@aleph0.info>
On Thu, May 23, 2024 at 12:37 AM Jeronimo Pellegrini <j_p@aleph0.info>
wrote:
> On 2024-05-22 18:07, Pierpaolo Bernardi wrote:
> > In chez:
> >
> >> (cond (else (define x 7) x))
> > 7
> >> x
> > 7
> >
> > which looks like a bug to me.
yes ,unless they wanted Chez scheme to behave as Python :-) :
if True:
x=7
x
7
You may check if x is defined outside of
> > the cond expression in the other implementations which do not raise an
> > error too?
>
> Sure! And the result varies a lot!
>
yes i spent many hours debugging to understand why
(cond (else (define y 7) y)) worked and not (cond (#t (define x 7) x))
just for info in Racket there is 'identifier-binding' to know if an
identifier is already bind, this is only useful in a macro syntax context
but it works great.
I'm able to remove all 'define' (and i think 'let') of the code, the code
testing itself if a variable is already binded and define it when necessary.
In summary this works with this macro:
#lang racket
(define-syntax (if-defined stx)
(syntax-case stx ()
[(_ id iftrue iffalse)
(let ([exist-id (identifier-binding #'id)])
(if exist-id #'iftrue #'iffalse))]))
(define-syntax :=
(lambda (stx)
(syntax-case stx ()
((_ var expr)
#'(if-defined var
(set! var expr)
(define var expr))))))
it works in any situation, and the checking of the binding of the
identifier seems to be done before the run-time (!) when in a program.
example at REPL but works in all my programs:
Welcome to DrRacket, version 8.13 [cs].
Language: racket, with debugging; memory limit: 8192 MB.
> (:= x 7)
> x
7
Welcome to DrRacket, version 8.13 [cs].
Language: racket, with debugging; memory limit: 8192 MB.
> (define x 7)
> (:= x 8)
> x
8
original code from:
https://stackoverflow.com/questions/20076868/how-to-know-whether-a-racket-variable-is-defined-or-not
> J.
>
next prev parent reply other threads:[~2024-05-23 14:25 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-22 19:26 cond clause does not allow definitions Damien Mattei
2024-05-22 20:07 ` Jeronimo Pellegrini
2024-05-22 20:33 ` Damien Mattei
2024-05-22 20:34 ` Damien Mattei
2024-05-22 20:37 ` lloda
2024-05-22 20:40 ` lloda
2024-05-22 21:07 ` Pierpaolo Bernardi
2024-05-22 22:09 ` Damien Mattei
2024-05-22 22:37 ` Jeronimo Pellegrini
2024-05-23 14:25 ` Damien Mattei [this message]
2024-05-23 14:59 ` Pierpaolo Bernardi
2024-05-22 22:12 ` Maxime Devos
2024-05-22 22:34 ` Damien Mattei
2024-05-24 14:27 ` Linus Björnstam
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='CADEOadfBZeJD=tBJq0gyfdDa=GA6iS3zP23jt1k44hSzKrxrPQ@mail.gmail.com' \
--to=damien.mattei@gmail.com \
--cc=guile-user@gnu.org \
--cc=j_p@aleph0.info \
--cc=olopierpa@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).