Your swap
function will work as:
(define (swap)
(let ((t 0))
(set! t x)
(set! x y)
(set! y t)))
because set!
is a syntactic keyword that evaluates its arguments specially. The first argument (t
in the first set!
expression in swap
above) is not evaluated; rather it is used to identify a location. The second argument (x
) is evaluated and, in this case, returns the value held in the location referenced by the identifier (x
).
The syntactic keyword define
works similarly. The first argument is not evaluated; it is used to create a new location; the second argument is evaluated. So, for your example, unquote
is not needed just use:
(define s x)
If you are trying to define s
so it returns the current value of x
rather then the value bound to x
when s
is allocated, then make s
a function:
(define (s) x)
Then the expression (s)
will return whatever x
is currently bound to.