[racket] Macro stepper expansion

From: Harry Spier (vasishtha.spier at gmail.com)
Date: Mon May 21 23:13:48 EDT 2012

I've entered the following example from the Racket Guide into DrRacket

#lang racket
(define-syntax-rule (swap X Y)
  (let ([tmp X])
    (set! X Y)
    (set! Y tmp)))

(let ([tmp 5] [other 6])
  (swap tmp other)
  (list tmp other))

It gives me the correct result:
> '(6 5)

But  when I stepped through it with the macro expander I got:

 [Macro transformation]

(module anonymous-module racket
  (#%module-begin
   (define-syntax-rule
    (swap X Y)
    (let ([tmp X]) (set! X Y) (set! Y tmp)))
   (let ([tmp 5] [other 6])
     (let ([tmp tmp]) (set! tmp other) (set! other tmp))
     (list tmp other))))

I was expecting to see something like the Racket Guide description in
section 16.1 I.e:
"Racket doesn’t produce the naive expansion for the above use of swap.
Instead, it produces
(let ([tmp 5]
      [other 6])
  (let ([tmp_1 tmp])
    (set! tmp other)
    (set! other tmp_1))
  (list tmp other))            "

While I do get the correct result, why does the macro expander show me
(let ([tmp tmp]  ..... instead of (let ([tmp_1 tmp] .... as the Racket
Guide says I should.

Thanks,
Harry


Posted on the users mailing list.