08-local-post3up

08-local-post3up - Local definitions and lexical scope...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
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). 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 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 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 ( 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 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 ( 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 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 ( 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 ....
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.

Page1 / 13

08-local-post3up - Local definitions and lexical scope...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online