[previous] [up] [next]     [index]
Next: Custodians Up: Parameters Previous: Random Numbers

Parameter Utilities

(make-parameter v [guard-proc]) returns a new parameter procedure. The value of the parameter is initialized to v in all threads. If guard-proc is supplied, it is used as the parameter's guard procedure. A guard procedure takes one argument. Whenever the parameter procedure is applied to an argument, the argument is passed on to the guard procedure. The result returned by the guard procedure is used as the new parameter value. A guard procedure can raise an exception to reject a change to the parameter's value.

(parameter? v) returns #t if v is a parameter procedure, #f otherwise.

(parameter-procedure=? a b) returns #t if the parameter procedures a and b always modify the same parameter, #f otherwise.

The parameterize form evaluates an expression with temporarily values installed for a group of parameters. The syntax of parameterize is:

  (parameterize ((parameter-expr value-expr)  tex2html_wrap_inline436748 ) body-expr  tex2html_wrap_inline436750 ) 
The result of a parameterize expression is the result of the last body-expr. The parameter-exprs determine the parameters to set, and the value-exprs determine the corresponding values to install before evalutaing the body-exprs. All of the parameter-exprs are evaluated first, then the value-exprs, and then the parameters are set.

After the body-exprs are evaluated, each parameter's setting is restored to its original value in the dynamic context of the parameterize expression. More generally, the values specified by the value-exprs determine initial ``remembered'' values, and whenever control jumps into or out of the body-exprs, the value of each parameter is swapped with the corresponding ``remembered'' value.

Examples:

  (parameterize ([exit-handler (lambda (x) 'no-exit)]) 
    (exit)) ; => no-exit

(define p1 (make-parameter 1)) (define p1 (make-parameter 2)) (parameterize ([p1 3] [p2 (p1)]) (cons (p1) (p2))) ; => (3 . 1)

(let ([k (let/cc out (parameterize ([p1 2]) (p1 3) (cons (let/cc k (out k)) (p1))))]) (if (procedure? k) (k (p1)) k)) ; => (1 . 3)


[previous] [up] [next]     [index]
Next: Custodians Up: Parameters Previous: Random Numbers

PLT