Unformatted text preview: rules like this are called lexical scoping rules, and the variables declared
in this way are called lexical variables.
Under lexical scoping, we can create a hole in a scope by redeclaring a variable.
We say the inner declaration shadows the outer one. For instance, in the example
above, the inner shadows the outer one in the multiplication
.
§¥
¨¤£ £ 3 We can illustrate lexical scoping with a contour diagram.
Contour diagram for our simple example:
§ ¤
¥ ¡ £ ¢!&% $#© §§
!¨
§§
!!§
¡ §¦ £ ! ¥ §§¥
!¨¤£ (let ((x 3)
(y 4))
(+ (let ((x
(+ y 5)))
(* x y))
x)) 4 ¥
¢ §¦ $#© §§
!¨£ A more complicated example
(lambda (x y) ; x1, y1 (let ((z (+ x y))) ; z1 (lambda (x z) ; x2, z2 (let ((x (let ((x ; x3, x4
(+ x y z)) ; line 5 (y 11)) ; y2 (+ x y z)))) ; line 7 (+ x y z) ) ) ) ) ; line 8 ¡ §¦
¥
¦£ At line 5, we are within the scope of the declarations
at depth 0
at depth 1
at depth 2
So at line 5,
is bound at depth 0 and position 0, and refers to
is bound at depth 2 and position 1, and refers to
is bound at depth 0 and position 1, and refers to
§ £ ¢
£
¢ §
¨ § ¦¢ ¥£ ¨
¢
¨
£ ¥ £ ¥ 5 §
¨ Here there are three occurrences of the expression
8 , on lines 5, 7, and (lambda (x y) ; x1, y1 (let ((z (+ x y))) ; z1 (lambda (x z) ; x2, z2 (let ((x (let ((x ; x3, x4
(+ x y z)) ; line 5 (y 11)) ; y2 (+ x y z))))
(+ x y z) ) ; line 7
) ) ) ; line 8 At line 7, we are within the scope of the declarations
at depth 0
at depth 1
at depth 2
at depth 3
So at line 7,
is bound at depth 0 and position 0, and refers to
is bound at depth 0 and position 1, and refers to
is bound at depth 1 and position 1, and refers to
§ ¦¢ ¥£
§ £ ¢
£
¢ §
¨ § ¦¢ ¥£ ¨ ¨
¤ £ ¥ £ ¥ 6 (lambda (x y) ; x1, y1 (let ((z (+ x y))) ; z1 (lambda (x z) ; x2, z2 (let ((x (let ((x ; x3, x4
(+ x y z)) ; line 5 (y 11)) ; y2 (+ x y z))))
(+ x y z) ) ; line 7
) ) ) ; line 8 At line 8, we are within the scope of the declarations
at depth 0
at depth 1
at depth 2
at depth 3
So at line 8,
is bound at depth 0 and position 0, and refers to
is bound at depth 3 and position 1, and refers to
is bound at depth 1 an...
View
Full Document
 Fall '09
 Local variable, Global variable, ¢¡

Click to edit the document details