{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

08-local-post3up

# 08-local-post3up - Local denitions and lexical scope...

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

Local definitions and lexical scope Readings: HtDP, Intermezzo 3 (Section 18). Language level: Intermediate Student CS 135 Fall 2009 08: Local definitions and lexical scope 1 Local definitions The functions and special forms we’ve seen so far can be arbitrarily nested—except define and check-expect . So far, definitions have to be made “at the top level”, outside any expression. The Intermediate language provides the special form local , which contains a series of local definitions plus an expression using them. ( local ( < def1 > ... < defn > ) < exp > ) What use is this? CS 135 Fall 2009 08: Local definitions and lexical scope 2 Motivating local definitions Consider Heron’s formula for the area of a triangle with sides a , b , c : s ( s - a )( s - b )( s - c ) , where s = ( a + b + c ) / 2 . It is not hard to create a Scheme function to compute this function, but it is difficult to do so in a natural fashion. We could notice that s - a = ( - a + b + c ) / 2 , and make similar substitutions. CS 135 Fall 2009 08: Local definitions and lexical scope 3

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

View Full Document
( define ( t-area a b c ) ( sqrt ( * ( / ( + a b c ) 2 ) ( / ( + ( - a ) b c ) 2 ) ( / ( + a ( - b ) c ) 2 ) ( / ( + a b ( - c )) 2 )))) This is short, but its relationship to Heron’s formula is unclear from just reading the code, and the technique does not generalize. We could instead use a helper function. CS 135 Fall 2009 08: Local definitions and lexical scope 4 ( define ( t-area2 a b c ) ( sqrt ( * ( s a b c ) ( - ( s a b c ) a ) ( - ( s a b c ) b ) ( - ( s a b c ) c )))) ( define ( s a b c ) ( / ( + a b c ) 2 )) CS 135 Fall 2009 08: Local definitions and lexical scope 5 This generalizes well to formulas that define several intermediate quantities. But the helper functions need parameters, which again makes the relationship to Heron’s formula hard to see. We could instead move the computation with a known value of s into a helper function, and provide the value of s as a parameter. CS 135 Fall 2009 08: Local definitions and lexical scope 6
( define ( t-area-helper a b c s ) ( sqrt ( * s ( - s a ) ( - s b ) ( - s c )))) ( define ( t-area3 a b c ) ( t-area-helper a b c ( / ( + a b c ) 2 ))) This is more readable, and shorter, but it is still awkward. The value of s is defined in one function and used in another. The local special form we introduced provides a natural way to bring these together. CS 135 Fall 2009 08: Local definitions and lexical scope 7 ( define ( t-area4 a b c ) ( local (( define s ( / ( + a b c ) 2 ))) ( sqrt ( * s ( - s a ) ( - s b ) ( - s c ))))) The local special form does not exist in standard Scheme, which instead provides several related language constructs. Each one of the standard constructs has simpler semantics, but none is as general as local . CS 135 Fall 2009 08: Local definitions and lexical scope 8 Since local is another form (like cond ) that results in double parentheses, you may wish to use square brackets to improve readability. This is another convention . ( define ( t-area4 a b c ) ( local [( define s ( / ( + a b c ) 2 ))] ( sqrt ( * s ( - s a ) ( - s b ) ( - s c ))))) CS 135 Fall 2009 08: Local definitions and lexical scope 9

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

View Full Document
Reusing names Local definitions permit reuse of names.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}