The first example isn't working as you imagined. Although both the x
parameter and the a
global variable are pointing to the same list, when you execute (set! x '(mutated))
you simply set x
(a parameter local to the procedure) to point to a different list, and a
remains unchanged. It'd be different if you wrote this:
(define (mutate-obj)
(set! a '(mutated)))
Now a
gets mutated inside the procedure. The second and third procedures are modifying the contents of the a
list, also pointed by x
, so the change gets reflected "outside" once the procedure returns.