r7rs-small-texinfo

R7RS-small Scheme converted to Texinfo.
git clone https://kaka.farm/~git/r7rs-small-texinfo
Log | Files | Refs | README | LICENSE

dynamic-bindings.texinfo (3189B)


      1 @node Dynamic bindings
      2 @subsection Dynamic bindings
      3 
      4 The dynamic extent of a procedure call is the time between when it is initiated and when it
      5 returns. In Scheme, call-with-current-continuation (section 6.10) allows reentering a
      6 dynamic extent after its procedure call has returned. Thus, the dynamic extent of a call
      7 might not be a single, continuous time period.
      8 
      9 This sections introduces parameter objects, which can be bound to new values for the
     10 duration of a dynamic extent. The set of all parameter bindings at a given time is called
     11 the dynamic environment.
     12 
     13 @deffn  procedure make-parameter init
     14 @deffnx procedure make-parameter init converter
     15 
     16 Returns a newly allocated parameter object, which is a procedure that accepts zero
     17 arguments and returns the value associated with the parameter object. Initially, this value
     18 is the value of @code{(@var{converter} @var{init})}, or of
     19 @var{init} if the conversion procedure
     20 @var{converter} is not specified. The associated value can be temporarily changed using
     21 parameterize, which is described below.
     22 
     23 The effect of passing arguments to a parameter object is implementation-dependent.
     24 @end deffn
     25 
     26 @deffn syntax parameterize (@svar{param@sub{1}} @svar{value@sub{1}})@dots{} @svar{body}
     27 
     28 Syntax: Both @svar{param@sub{1}} and @svar{value@sub{1}} are expressions.
     29 
     30 It is an error if the value of any @svar{param} expression is not a parameter object.
     31 
     32 Semantics:
     33 A @code{parameterize} expression is used to change the values returned by
     34 specified parameter objects during the evaluation of the body.
     35 
     36 The @svar{param} and @svar{value} expressions
     37 are evaluated in an unspecified order.  The @svar{body} is
     38 evaluated in a dynamic environment in which calls to the
     39 parameters return the results of passing the corresponding values
     40 to the conversion procedure specified when the parameters were created.
     41 Then the previous values of the parameters are restored without passing
     42 them to the conversion procedure.
     43 The results of the last
     44 expression in the @svar{body} are returned as the results of the entire
     45 @code{parameterize} expression.
     46 
     47 Note: If the conversion procedure is not idempotent, the results of
     48 @code{(parameterize ((x (x))) @dots{})},
     49 which appears to bind the parameter @var{x} to its current value,
     50 might not be what the user expects.
     51 
     52 If an implementation supports multiple threads of execution, then
     53 @code{parameterize} must not change the associated values of any parameters
     54 in any thread other than the current thread and threads created
     55 inside @svar{body}.
     56 
     57 Parameter objects can be used to specify configurable settings for a
     58 computation without the need to pass the value to every
     59 procedure in the call chain explicitly.
     60 
     61 @lisp
     62 (define radix
     63   (make-parameter
     64    10
     65    (lambda (x)
     66      (if (and (exact-integer? x) (<= 2 x 16))
     67          x
     68          (error "invalid radix")))))
     69 
     70 (define (f n) (number->string n (radix)))
     71 
     72 (f 12)                    @result{} "12"
     73 (parameterize ((radix 2))
     74   (f 12))                 @result{} "1100"
     75 (f 12)                    @result{} "12"
     76 
     77 (radix 16)                @result{} @r{unspecified}
     78 
     79 (parameterize ((radix 0))
     80   (f 12))                 @result{} @r{error}
     81 @end lisp
     82 
     83 @end deffn