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.
2))
) (+ (x) 1))
)
(parameterize ([x 5])
(incr-x)))
(make-parameter
make-parameter
creates a function x
that returns the value.
Evaluating (x)
results in 2
.
(incr-x) ;=> 3
However, you can associate a new value to the x
parameter using
parameterize
:
(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.