On Thu, May 23, 2024 at 12:37 AM Jeronimo Pellegrini <j...@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.
>

Reply via email to