2
votes

I've got this macro:

(define-syntax inspect
  (syntax-rules ()
    [(_ x) (printf "~a is: ~a\n" 'x x)]))

Which works like so:

(let ([x 2]) (inspect x))
>> x is: 2

But what I'd like to do is extend it so that I have

(_ x ...)

and loop through the various values providing, printing out their variable names and values accordingly.

ie

  (let ([x 2] [y 3]) (inspect x y))
    >> x is: 2
    y is: 3

I'm a bit stuck on this part.

For example, with something like this:

(define-syntax (inspect stx)
  (datum->syntax
   stx
   (for ([i (cdr (syntax->list stx))])
     (printf "~a is: ~a" (syntax->datum i) i))))

I'm unsure how to get at the value of i, on the last line.

Any help is appreciated.

Thanks.

2

2 Answers

3
votes

There's no need to have a two-pattern rule -- you can do the whole thing with a begin:

(define-syntax-rule (inspect x ...)
  (begin (printf "~a is: ~a\n" 'x x) ...))
2
votes

Here's one solution:

(define-syntax inspect
  (syntax-rules ()
    [(_ x) (printf "~a is: ~a\n" 'x x)]
    [(_ x y ...) (begin (inspect x) (inspect y ...))]))

Note that the ordering of clauses is important.