I'm in no ways a macro expert, but will this work? It uses identifier-binding 
to check if the identifier for the hash table is bound.

(define-syntax (set/define stx)
  (syntax-case stx ()
    [(_ ht key value)
     (cond [(identifier-binding #'ht)
            #'(hash-set! ht key value)]
           [else
            #'(begin
                (define ht (make-hash))
                (hash-set! ht key value))])]))

On Friday, April 19, 2019 2:33:54 PM PDT zeRusski wrote:
> Not quite what I had in mind. The following examples must all work without
> errors:
> 
> #lang racket
> 
> ;; set/define defined here
> 
> (set/define h a 1)
> (set/define h a 2)
> (hash-set! h 'b 42)
> 
> (define bar (make-hash))
> (set/define bar a 1)
> 
> (define (foo)
>   (set/define local-h a 1)
>   (set/define local-h a 2)
>   (hash-set! local-h 'b 42)
>   local-h)
> 
> (foo)
> 
> Basically, if the identifier h is unbound at runtime
>   (set/define h key val)
> is the same as
>  (define h (make-hash))
>  (hash-set! h 'key val)
> 
> If h is bound
>  (hash-set! h 'key val)


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to