Dynamic bindings
The dynamic extent of a procedure call is the time between
when it is initiated and when it returns. In Scheme, This sections introduces parameter objects, which can be bound to new values for the duration of a dynamic extent. The set of all parameter bindings at a given time is called the dynamic environment.
(make-parameter
init
)
procedure
(make-parameter
init converter
)
procedure
Returns a newly allocated parameter object,
which is a procedure that accepts zero arguments and
returns the value associated with the parameter object.
Initially, this value is the value of
(converter init) , or of init
if the conversion procedure converter is not specified.
The associated value can be temporarily changed
using parameterize , which is described below.
The effect of passing arguments to a parameter object is implementation-dependent.
(parameterize
(({param1} {value1}) ...)
{body})
syntax
Syntax:
Both param1 and value1 are expressions.
It is an error if the value of any param expression is not a parameter object.
Semantics:
A
The
Note: If the conversion procedure is not idempotent, the results of
Parameter objects can be used to specify configurable settings for a computation without the need to pass the value to every procedure in the call chain explicitly.
(define radix
(make-parameter
10
(lambda (x)
(if (and (exact-integer? x) (<= 2 x 16))
x
(error "invalid radix")))))
(define (f n) (number->string n (radix)))
(f 12) ==> "12"
(parameterize ((radix 2))
(f 12)) ==> "1100"
(f 12) ==> "12"
(radix 16) ==> unspecified
(parameterize ((radix 0))
(f 12)) ==> error
|