Building Little Languages with Macros
By Matthias Felleisen, Robert Bruce Findler, Matthew Flatt and and Shriram Krishnamurthi, April 01, 2004
Source Code Accompanies This Article. Download It Now.
Pattern-based macros in Scheme can express interesting language extensions.
Apr04: Building Little Languages With Macros
<b>(a)</b>
(define-cbr (f a b)
(swap a b))
; Produces 2:
(let ((x 1) (y 2))
(f x y)
x)
<b>(b)</b>
(define (do-f get<sub>1</sub> get<sub>2</sub> set<sub>1</sub> set<sub>2</sub>)
(define-get/set-var a get<sub>1</sub> set<sub>1</sub>)
(define-get/set-var b get<sub>2</sub> set<sub>2</sub>)
(swap a b))
<b>(c)</b>
(let ((x 1) (y 2))
(f x y)
x)
<b>(d)</b>
(let ((x 1) (y 2))
(do-f (lambda () x)
(lambda () y)
(lambda (v) (set! x v))
(lambda (v) (set! y v)))
x)
<b>(e)</b>
(begin
(define (do-f get1 get2 set1 set2)
(define-get/set-var a get1 set1)
(define-get/set-var b get2 set2)
(swap a b))
(define-syntax f
(syntax-rules ()
((f actual ...)
(do-f (lambda () actual)
...
(lambda (v)
(set! actual v))
...) ))))
Example 8: Call-by-reference.