[Chicken-users] the effect of set! on the top-level namespace

From:

Terrence Brannon

Subject:

[Chicken-users] the effect of set! on the top-level namespace

Date:

Mon, 15 Oct 2007 10:42:03 -0400

I would like to know:
* what is happening to the symbol afunc in line 1 versus line 3. In
particular is the same memory location being over-written?
* Also, in line 2 is first-func being set to a value which is not destroyed by
the set! call in line 3?
* I think I was anticipating that first-func would assume the behavior
of second-func once afunc was re-defined.
* Finally, there is no need to make things complicated by using set!
since the define version has the same behavior. But for some code I am
writing personally, I have to use set! because define inside a let
does not seem to affect the top-level the way I need it to.
(set! afunc (lambda (x) (+ x 5))) ;;; line 1
(set! first-func afunc)
(set! afunc (lambda (x) (* x 1000))) ;;; line 3
(set! second-func afunc)
(first-func 1)
(second-func 1)
(afunc 1) ; same as second-func
;;; define version
(define afunc (lambda (x) (+ x 5)))
(define first-func afunc)
(define afunc (lambda (x) (* x 1000)))
(define second-func afunc)
(first-func 1)
(second-func 1)
(afunc 1) ; same as second-func