This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 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 weve seen so far can be arbitrarily nestedexcept define and checkexpect . 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 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 2009 08: Local definitions and lexical scope 3 ( define ( tarea 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 Herons 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 ( tarea2 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 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 2009 08: Local definitions and lexical scope 6 ( define ( tareahelper a b c s ) ( sqrt ( * s ( s a ) ( s b ) ( s c )))) ( define ( tarea3 a b c ) ( tareahelper 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 ( tarea4 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 ....
View
Full
Document
This note was uploaded on 02/12/2010 for the course CS 135 taught by Professor Vasiga during the Fall '07 term at Waterloo.
 Fall '07
 VASIGA

Click to edit the document details