[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

binding concepts



Hi,
What does DrSheme (or any other Scheme) do when binding or assigning a
variable? These eare two different things. If I am well informed, binding
means the creation of an association between a variable and a (fresh)
storage location, whereas assignment means to store a value in the location
a variable is bound to. The practice that binding is usually immediately
followed by assigment need cause no confusion. For instance, application of
a lambda form causes the formal arguments both to be bound and to be
assigned in their new bindings. Strictlty spoken this means that altering
the value of one variable cannot cause a change of the value of another
variable. Even if values are internally represented by pointers to the
values proper in stead of the storage location, this cannot cause confusion
(disregarding the special effects of things like setcar! and setcdr!, of
course, that intentionally operate on the values themselves). But confusion
can arise, at least with me, if values can be made to change themselves. No
harm done as long as no variables are changed outside the scope of the code
causing the change. Below you find some examples. Consider the the first
one. Variable copy1 is affected by code outside its scope. Please don't
explain how this can happen. I think I understand. The variable is part of
keep-copy-of-counter1. The latter is visible within function counter2.
Nevertheless, I wonder whether or not this is conceptually correct? Do other
pubicly available versions of Scheme exhibit the same behaviour?
Jos

(define counter1
 (let ((n 0))
  (lambda () (begin (set! n (add1 n)) n))))
(define keep-copy-of-counter1
 (let ((copy1 counter1)) (lambda () copy1)))
(list
 (counter1) (counter1) (counter1)
 ((keep-copy-of-counter1))
 (eq? counter1 (keep-copy-of-counter1)))
 ; --> (1 2 3 4 #t)

(define counter2
 (let ((n 0))
  (lambda () (begin (set! n (add1 n)) n))))
(define keep-copy-of-counter2 'who-cares?)
(set! keep-copy-of-counter2 (let ((copy2 counter2)) (lambda () copy2)))
(list
 (counter2) (counter2) (counter2)
 ((keep-copy-of-counter2))
 (eq? counter2 (keep-copy-of-counter2)))
 ; --> (1 2 3 4 #t)

(let*
 ((counter3
   (let ((n 0))
    (lambda () (begin (set! n (add1 n)) n))))
  (keep-copy-of-counter3
   (let ((copy3 counter3)) (lambda () copy3))))
 (list
  (counter3) (counter3) (counter3)
  ((keep-copy-of-counter3))
  (eq? counter3 (keep-copy-of-counter3))))
 ; --> (1 2 3 4 #t)

(define make-counter4
 (let
  ((make-counter4
    (lambda (n)
     (lambda ()
      (begin (set! counter4 (make-counter4 (add1 n))) n)))))
   make-counter4))
(define counter4 (make-counter4 1))
(define keep-copy-of-counter4
 (let ((copy4 counter4)) (lambda () copy4)))
(list
 (counter4) (counter4) (counter4)
 ((keep-copy-of-counter4))
 (eq? counter4 (keep-copy-of-counter4)))
 ; --> (1 2 3 1 #f)

(define make-counter5
 (let
  ((make-counter5
    (lambda (n)
     (lambda ()
      (begin (set! counter5 (make-counter5 (add1 n))) n)))))
   make-counter5))
(define counter5 (make-counter5 1))
(define keep-copy-of-counter5
 (let ((copy5 counter5)) (lambda () copy5)))
(list
 (counter5) (counter5) (counter5)
 ((keep-copy-of-counter5))
 (eq? counter5 (keep-copy-of-counter5)))
 ; --> (1 2 3 1 #f)

(define make-counter6
 (let
  ((make-counter6
    (lambda (n)
     (lambda ()
      (begin (set! counter6 (make-counter6 (add1 n))) n)))))
   make-counter6))
(define counter6 (make-counter6 1))
(define keep-copy-of-counter6 'who-cares?)
(set! keep-copy-of-counter6 (let ((copy6 counter6)) (lambda () copy6)))
(list
 (counter6) (counter6) (counter6)
 ((keep-copy-of-counter6))
 (eq? counter6 (keep-copy-of-counter6)))
 ; --> (1 2 3 1 #f)

I Don't think Newton told us why stones come down on the ground, although he
told us how.
Jacob J. A. Koot (Jos)