This is the end of the preview.
Sign up
to
access the rest of the document.

Unformatted text preview: Local definitions and lexical scope Readings: HtDP, Intermezzo 3 (Section 18). We move to the Intermediate teaching language with the introduction of local definitions. Our initial claims for this new language construct will be modest, but in fact this feature, together with a broader definition of values given in the next module, will considerably improve the expressivity of our code. CS 135 Fall 2008 07: Local definitions and lexical scope 1 Local definitions The functions and special forms weve seen so far can be arbitrarily nested except define . 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 ( &amp;lt; def1 &amp;gt; ... &amp;lt; defn &amp;gt; ) &amp;lt; exp &amp;gt; ) What use is this? CS 135 Fall 2008 07: Local definitions and lexical scope 2 Motivating local definitions Consider Herons formula for the area of a triangle with sides a , b , c : p 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 2008 07: Local definitions and lexical scope 3 ( define ( t-area a b c ) ( sqrt ( * ( / ( + a b c ) 2 ) ( / ( + (- a ) b c ) 2 ) ( / ( + a (- b ) c ) 2 ) ( / ( + a b (- c )) 2 )))) While this is short, its relationship to Herons formula is unclear, and we may not be as fortunate with other such substitutions. We could instead use a helper function. CS 135 Fall 2008 07: 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 2008 07: 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 Herons 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 2008 07: 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 2008 07: 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....
View
Full Document