Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language

Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Scheme Programming Language

fluid binding

Does a fluid binding override the binding in a closure?

(let ((x 4))
  (let ((p (lambda (y) (+ x y))))
    (+ (fluid-let ((x 7)) (p 1)) (p 2))))
;=> 14

(let ((x 0))
  ((lambda (p) (+ (fluid-let ((x 7)) (p 1)) (p 2)))
   (let ((x 4)) (lambda (y) (+ x y)))))
;=> 11

- - - - - - - - - -
"Some Scheme implementations support a controlled form of assignment known
as fluid binding, in which a variable takes on a temporary value during a
given computation and reverts to the old value after the computation has
completed. The syntactic form fluid-let defined below in terms of
dynamic-wind permits the fluid binding of a single variable x to a value v
within a sequence of expressions e1 e2 .... "

(define-syntax fluid-let
  (syntax-rules ()
    ((_ ((x v)) e1 e2 ...)
     (let ((y v))
       (let ((swap (lambda () (let ((t x)) (set! x y) (set! y t)))))
         (dynamic-wind swap (lambda () e1 e2 ...) swap))))))


- - - - - - - - - -
"We suggested earlier the use of assignment to make a program more modular
by allowing one procedure to communicate information to a distant procedure
without requiring intermediate procedures to be aware of it. Very often such
an assignment should only be temporary, lasting for the execution of a
procedure call." [...] setdynamic-exp (id rhs-exp body) "The effect of the
setdynamic expression is to assign temporarily the value of rhs-exp to id,
evaluate body, re-assign original value, and return the value of body."

EOPL Exercise 3.47 page 106-107

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc