One way to do dynamic scope is with fluid-let, which basically reassigns the value of a variable instead of creating a shadow, but though this is available in Scheme, it’s not available in Racket.
Racket has a form named parameterize, that allows you to pass variables dynamically, but forces you to set a default value.
(define x (make-parameter 2)) (define (incr-x) (+ (x) 1)) (define (dynamic-x) (parameterize ([x 5]) (incr-x)))
make-parameter creates a function
x that returns the value. Evaluating
(incr-x) ;=> 3
However, you can associate a new value to the
x parameter using
(dynamic-x) ;=> 6
An advantage of
parameterize over imperative updates of parameter values is
that the implementation is thread-safe, and continuation-friendly. There are a
few other pluses in addition to that, which you can check out in the docs.