[plt-scheme] Question about fluid-let

From: Greg Woodhouse (gregory.woodhouse at sbcglobal.net)
Date: Fri Dec 30 13:02:07 EST 2005

Okay. I'm not sure I understand the explanation of fluid-let. I defined
bump-counter this way

(define bump-counter
  (lambda ()
     (set! counter (+ counter 1))
     counter))

and then did the following

> (define counter 100)
> counter
100
> (define counter 1)
> (fluid-let ((counter 99))
    (display (bump-counter)) (newline)
    (display (bump-counter)) (newline)
    (display (bump-counter)) (newline))
100
101
102
> counter
1
> (let ((counter 99))
    (display (bump-counter)) (newline)
    (display (bump-counter)) (newline)
    (display (bump-counter)) (newline))
2
3
4
> 

This is not what I expected. My only thought that the scope of counter
in the definition of bump-counter is unrelated to the scope introeuced
by let in the second example, so bump-counter affects the binding in
effect when it was defined, not when it is used. Is this what is
referred to as a closure?

Anyway, I wonder if fluid-let doesn't actually introdeuce what I'd call
a dynamic scope in Perl, Python or MUMPS (meaning the binding is
accessed via a dynamic link), or am I on the wrong track?

===
Gregory Woodhouse  <gregory.woodhouse at sbcglobal.net>
"Interaction is the mind-body problem of computing."
--Philip L. Wadler


Posted on the users mailing list.