{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter 5

# Chapter 5 - Teach Yourself Scheme in Fixnum Days[Go to...

This preview shows pages 1–3. Sign up to view the full content.

Teach Yourself Scheme in Fixnum Days [Go to first , previous , next page; contents ; index ] Chapter 5 Lexical variables Scheme's variables have lexical scope, ie, they are visible only to forms within a certain contiguous stretch of program text. The global variables we have seen thus far are no exception: Their scope is all program text, which is certainly contiguous. We have also seen some examples of local variables. These were the lambda parameters, which get bound each time the procedure is called, and whose scope is that procedure's body. Eg, (define x 9) (define add2 (lambda (x) (+ x 2))) x => 9 (add2 3) => 5 (add2 x) => 11 x => 9 Here, there is a global x , and there is also a local x , the latter introduced by procedure add2 . The global x is always 9 . The local x gets bound to 3 in the first call to add2 and to the value of the global x , ie, 9 , in the second call to add2 . When the procedure calls return, the global x continues to be 9 . The form set! modifies the lexical binding of a variable. (set! x 20) modifies the global binding of x from 9 to 20 , because that is the binding of x that is visible to set! . If the set! was inside add2 's body, it would have modified the local x : (define add2 (lambda (x) (set! x (+ x 2)) file:///C|/Documents%20and%20Settings/Linda%20Grauer. ..otes/Teach%20Yourself%20Scheme/t-y-scheme-Z-H-7.html (1 of 5) [2/6/2008 11:39:50 AM]

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Teach Yourself Scheme in Fixnum Days x)) The set! here adds 2 to the local variable x , and the procedure returns this new value of the local x . (In terms of effect, this procedure is indistinguishable from the previous
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 5

Chapter 5 - Teach Yourself Scheme in Fixnum Days[Go to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online