[racket] Call by reference macro example in Racket Guide

From: Harry Spier (vasishtha.spier at gmail.com)
Date: Wed Dec 14 15:19:56 EST 2011

Dear list members,

In the Racket Guide 16.1.7 Extended Example: Call-by-Reference Functions
it shows how with macros to set up call by reference versions of
Racket functions.

I'm a little bit confused on why for the example, the macro (swap x y)
was used as the example function to turn into a call-by-reference
function, since doesn't the macro (swap x y) already act like a
call-by-reference function.

Ie. i the example I can't see any difference between the effects of:
(swap x y)
and the effects of:
(define-cbr (swap-by-reference x y) (swap x y))
(swap-by-reference x y)

In this particular case is there some difference in the effects of
(swap x y) and (swap-by-reference x y) ?

Here is the full code of the example as I've entered it into DrRacket

#lang racket

(define-syntax-rule (swap x y)
  (let ([tmp x])
    (set! x y)
    (set! y tmp)))

(define-syntax-rule (define-get/put-id id get put!)
  (define-syntax id
    (syntax-id-rules (set!)
      [(set! id e) (put! e)]
      [(id a (... ...)) ((get) a (... ...))]
      [id (get)])))

(define-syntax f
  (syntax-rules ()
    [(id actual ...)
     (do-f (lambda () actual)
           (lambda (v)
             (set! actual v))

(define-syntax-rule (define-cbr (id arg ...) body)
    (define-syntax id
      (syntax-rules ()
        [(id actual (... ...))
         (do-f (lambda () actual)
               (... ...)
               (lambda (v)
                 (set! actual v))
               (... ...))]))
    (define-for-cbr do-f (arg ...)
      () ; explained below...

(define-syntax define-for-cbr
  (syntax-rules ()
    [(define-for-cbr do-f (id0 id ...)
       (gens ...) body)
     (define-for-cbr do-f (id ...)
       (gens ... (id0 get put)) body)]
    [(define-for-cbr do-f ()
       ((id get put) ...) body)
     (define (do-f get ... put ...)
       (define-get/put-id id get put) ...

(define-cbr (swap-by-ref a b)
  (swap a b))

(let ([x 1] [y 2])
  (swap-by-ref x y)
  (list x y))

(let ([x 1] [y 2])
  (swap x y)
  (list x y))

Posted on the users mailing list.