Thanks both for your replies, but it doesn't look like either of these will do 
exactly what I want. While the code keeping track of id's that you wrote comes 
close, it doesn't allow for shadowing. I still want racket to do everything 
that it used to do, so the program


(define a 3)
(let ()
  (define a 4)
  a)

should still be valid. Shadowing is okay, I just can't have two duplicate 
identifiers in the same scope. I suppose I could use your code and clear the 
id-set every time I entered a new scope, but at that point, I'm basically 
keeping track of the entire environment and there's not a huge difference 
between just implementing my own lexical scope. I'd like to just have racket 
tell me when there are duplicate identifiers, as it has to know to throw it's 
own errors.

On Tuesday, July 18, 2017 at 2:08:17 PM UTC-4, Jens Axel Søgaard wrote:
> #lang racket
> 
> 
> (require (for-syntax syntax/parse syntax/id-set))
> 
> 
> (begin-for-syntax
>   (define defined-ids          (mutable-free-id-set))
>   (define (already-defined? x) (free-id-set-member? defined-ids x))
>   (define (define-it! x)       (free-id-set-add!    defined-ids x)))
> 
> 
> (define-syntax (def stx)
>   (syntax-parse stx
>     [(_def x:id e:expr)
>      (when (already-defined? #'x)
>        (raise-syntax-error 'def "identifier defined twice" #'stx #'x))
>      (define-it! #'x)
>      (syntax/loc stx
>        (define x e))]))
> 
> 
> (def one 1)
> (def two (+ one one))
> (def three (+ one two))
> (def three (+ two one))
> 
> 
> 
> 
> 2017-07-18 19:23 GMT+02:00 Matthias Felleisen <matt...@ccs.neu.edu>:
> 
> 
> 
> 
> #lang racket
> 
> 
> 
> (define my-favorite-numbers '(1 2 3))
> 
> 
> 
> (define (duplicate-exists? n)
> 
>   (member n my-favorite-numbers))
> 
> 
> 
> (duplicate-exists? 3)
> 
> (duplicate-exists? 4)
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> > On Jul 17, 2017, at 3:43 AM, Sam Waxman <samw...@gmail.com> wrote:
> 
> >
> 
> > Hello,
> 
> >
> 
> > In the regular #racket, the following program
> 
> >
> 
> > (define a 1)
> 
> > (define a 2)
> 
> >
> 
> > will result in a syntax error letting you know that you have a duplicate 
> > identifier. I would like to make my own define that throws a custom error 
> > message in this case. I.e.
> 
> >
> 
> > (define-syntax (my-define stx)
> 
> >  (syntax-parse stx
> 
> >    [(_ id:id expr) (begin
> 
> >                    (if (duplicate-exists? id)
> 
> >                        (syntax-error "Oh no!")
> 
> >                        (void))
> 
> >                   #'(define id expr)]))
> 
> >
> 
> > I know there's a lot of functions like bound-identifier=? and 
> > check-duplicate-identifier, but they seem to all be geared towards the user 
> > putting in all the identifiers in question as function arguments. In this 
> > case, I need a function like
> 
> >
> 
> > (duplicate-exists? id)
> 
> >
> 
> > that just takes in the single identifier, and checks to see if, in the 
> > scope the function is invoked, there's a duplicate identifier already 
> > there. I just can't seem to figure out how to write that function.
> 
> >
> 
> > Thanks in advance!
> 
> >
> 
> > --
> 
> > 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 racket-users...@googlegroups.com.
> 
> > For more options, visit https://groups.google.com/d/optout.
> 
> 
> 
> --
> 
> 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 racket-users...@googlegroups.com.
> 
> For more options, visit https://groups.google.com/d/optout.
> 
> 
> 
> 
> 
> -- 
> 
> -- 
> Jens Axel Søgaard

-- 
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 racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to