Section 18

How to Design Programs: An Introduction to Programming and Computing

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

View Full Document Right Arrow Icon
How to Design Programs: An Introduction to Computing and Programming [Go to first , previous , next page; contents ; index ] Section 18 Intermezzo 3: Local Definitions and Lexical Scope [../icons/plt.gif] Intermediate Student Programs do not just consist of single definitions. In many cases, a program requires the definition of auxiliary functions or of functions with mutual references. Indeed, as we become more experienced, we write programs that consist of numerous auxiliary functions. If we are not careful, these large collections of functions overwhelm us. As the size of our functions grows, we need to organize them so that we (and other readers) can quickly identify the relationships between parts. This section introduces local , a simple construct for organizing collections of functions. With local , a programmer can group function definitions that belong together so that readers immediately recognize the connection between the functions. Finally, the introduction of local also forces us to discuss the concept of variable binding. While the variable and function definitions of Beginning Student Scheme already introduce bindings into a program, a good understanding of local definitions is possible only with a thorough familiarity of this concept. 18.1 Organizing Programs with local A local -expression groups together an arbitrarily long sequence of definitions similar to those found in the Definitions window. Following our established rules, we first introduce the syntax and then the semantics and pragmatics of local -expressions. Syntax of local A local -expression is just another kind of expression: <exp> = ( local ( <def-1> ...<def-n> ) <exp> ) As usual, <def-1> ... <def-n> is an arbitrarily long sequence of definitions (see figure 51 ) and <exp> is an arbitrary expression. In other words, a local -expression consists of the keyword local , followed by a sequence of definitions grouped with ( and ) , followed by an expression. file:///C|/Documents%20and%20Settings/Linda%20Graue. ..How%20to%20Design%20Programs/curriculum-Z-H-23.html (1 of 19) [2/5/2008 4:48:59 PM]
Background image of page 1

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

View Full DocumentRight Arrow Icon
How to Design Programs: An Introduction to Computing and Programming <def> = ( define ( <var> <var> ...<var> ) <exp> ) | ( define <var> <exp> ) | ( define-struct <var> ( <var> ...<var> )) Figure 51: Scheme definitions The keyword local distinguishes this new class of expressions from other expressions, just as cond distinguishes conditional expressions from applications. The parenthesized sequence that follows local is referred to as the LOCAL DEFINITION .The definitions are called the LOCALLY DEFINED variables, functions, or structures. All those in the Definitions window are called TOP-LEVEL DEFINITIONS. Each name may occur at most once on the left-hand side, be it in a variable definition or a function definition. The expression in each definition is called the RIGHT-HAND SIDE expression. The expression that follows the definitions is the BODY. Let us take a look at an example:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This test prep was uploaded on 02/06/2008 for the course CS 1102 taught by Professor Fisler during the Spring '07 term at WPI.

Page1 / 19

Section 18 - How to Design Programs: An Introduction to...

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

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