Local definitions and lexical scope
Readings: HtDP , Intermezzo 3 (Section 18). Language level: Intermediate Student Topics: Motivating local definitions Semantics of local Reasons to use local Terminology
Motivation Semantics Reasons Terminology
1/39 12: Local Definitions CS 135
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 [(define x_1 exp_1) ... (define x_n exp_n)] bodyexp)
What use is this?
Motivation Semantics Reasons Terminology
2/39 12: Local Definitions CS 135
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 Racket function to compute this function, but it is difficult to do so in a clear and natural fashion. We will describe several possibilities, starting with a direct implementation.
Motivation Semantics Reasons Terminology