(a)
(swap x y)

(b)
(let ((tmp1 x))
   (set! y x)
   (set! x tmp1))

(c)
(swap tmp other)

(d)
(let ((tmp1 tmp))
   (set! other tmp)
   (set! tmp tmp1))

(e)
(define (f x)
   (define-syntax swap-with-arg
      (syntax-rules ()
         ((swap-with-arg y) (swap x y))))
   (let ((z 12)
         (x 10))
     ; Swaps z with original x:
     (swap-with-arg z)))

(f) 
(define (f x)
   (let ((z 12)
         (x1 10))
    (swap x z)))

Example 3: Expanding swap.

Back to Article