> Expanding `y-coord` by itself leads to the error.

I've tripped over this, so just to elaborate slightly.

`syntax-id-rules` will work, but your current macro:

;;;;;;;;;;;;;;;

(define-syntax y-coord
  (syntax-id-rules (map)
    [(map y-coord lst) (map (lambda (p)
                              (point-y p)) 
                            lst)]))
;;;;;;;;;;;;;;;

assumes that it will be getting a syntax pattern that looks like `(map y-coord 
lst)`. 

But it will not. The pattern it gets is simply `y-coord`. (More broadly, 
identifier macros made with `syntax-id-rules` will match only three kinds of 
patterns: the name alone, the name after an open parenthesis, or the name 
within `set!` Killer docs cross-reference: [1]) 

So you can repair your macro by matching to `y-coord` as a pattern:

;;;;;;;;;;;;;;;

#lang racket
(require rackunit)

(define-syntax y-coord
  (syntax-id-rules ()
    [y-coord point-y]))

(struct point (x y))
(define points (list (point 25 25) (point 33 54) (point 10 120)))

(check-equal? (map y-coord points) '(25 54 120))

;;;;;;;;;;;;;;;


BTW the "bad syntax" error is a generic error you get when there's no pattern 
match inside a macro. Thus, it can be wise to include an `else` branch so that 
you can pinpoint the source, for instance:


;;;;;;;;;;;;;;;

(define-syntax y-coord
  (syntax-id-rules (map)
    [(map y-coord lst) (map (lambda (p)
                              (point-y p)) 
                            lst)]
    [else (error 'no-pattern-matched-in-your-y-coord-macro)]))

;;;;;;;;;;;;;;;



[1] 
http://docs.racket-lang.org/guide/pattern-macros.html?q=identifier%20macro#%28part._.Identifier_.Macros%29

-- 
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