[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_inline100181 ) body-expr  tex2html_wrap_inline100183 ) 
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 evaluating the body-exprs. All of the parameter-exprs are evaluated first (checked with check-parameter-procedure), then all value-exprs are evaluated, 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)

(check-parameter-procedure v) returns v if it is a procedure that can take both 0 arguments and 1 argument, and raises exn:application:type otherwise. The check-parameter-procedure procedure is used in the expansion of parameterize.


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

PLT