Hello,

In Scheme:

(define a 10)

(list a) => (10)

(let ((a 20))
  (list a)) => (20)

(let ((a 30))
  (set! a 40)
  (list a)) => (40)

; The a established by the (a 30) inside the let is set.

(list a) => 10

; The top-level a is still 10

(let ()
  (set! a 50))

(list a) => (50)

; Even though we ran set! inside a new scope, the top-level a was changed. 
This is because there is no new a binding in the let to shadow the top-level 
a.

SYMBOL: a

10 a set

[ 20 a set   a get ] with-scope => 20

a get => 10

This is not like Scheme. a is set inside the scope but it doesn't affect the 
top-level a. There isn't a let binding to shadow the top-level. This 
behaviour is a result of the set word which *always* modifies the top of the 
namestack.

So let's make set* which searches up the namestack until it find the variable 
and modifies it there.

! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

USING: kernel kernel-internals sequences hashtables ;

: set-hash-stack ( value key seq -- )
dupd [ hash-member? ] find-last-with nip set-hash ;

: set* ( val var -- ) namestack* set-hash-stack ;

! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

10 a set

[ 20 a set*   a get ] with-scope => 20

a get => 20

! set* modified the a variable in the namespace one level up.

Factor beginners, if you feel lost, it's probably because you haven't explored 
the namestack, what it is, where it is, how things modify it. Read 
the "Variables and Namespaces" section of the manual and experiment with the 
namestack*, set, get, >n, ndrop words. Then see how with-scope is defined in 
terms of >n and ndrop. Finally, come back and understand set-hash-stack and 
set*.

Ed

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to