Scheme: An Updating set! Defined

set!!

Often set! is used to update a variable as a function of its current value. We define set!! to signal the particular situation where the new value is a function of the current with the current as first argument. We can then remove the repetition of the variable name, and the parentheses signalling the procedure call.

To express, e.g., (set! x (+ x 1)) we have two choices of "x" to remove, yielding either:

We choose the latter to preserve the prefix notation of procedure calls, and view the extra ! as modifying the + into a mutating call-by-name (of its first argument).

;;; Update variable based on its current value.
;
;     (set!! op v a ...) sets v to (op v a ...)
;
(define-syntax set!!
  (syntax-rules () ((_ op v a ...) (set! v (op v a ...)))))