bryan garza
Dynamic Scope In Racket 2016-03-10

Table of contents

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 (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.