Section 20

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 20 Functions are Values The functions of section 19 stretch our understanding of evaluation. It is easy to understand how functions consume numbers and symbols; cosuming structures and lists is a bit more complicated, but still within our grasp; but functions consuming functions is a strange idea. As a matter of fact, the functions of section 19 violate the Scheme grammar of section 8 . In this section, we discuss how to adjust Scheme's grammar and evaluation rules so that we can understand the role of functions as data or values. Without a good understanding of these ideas, we cannot hope to abstract functions. Once we understand these ideas, we can turn to the problem of writing contracts for such functions. Finally, the last part of the section introduces functions that produce functions, another powerful abstraction technique. 20.1 Syntax and Semantics The abstract functions of section 19 violate Scheme's basic grammar in two ways. First, the names of functions and primitive operations are used as arguments in applications. An argument, though, is an expression, and the class of expressions does not contain primitive operations and function names. It does contain variables, but we agreed that they are only those variables mentioned in variable definitions and as function parameters. Second, parameters are used as if they were functions, that is, the first position of applications. But the grammar of section 8 allows only the names of functions and primitive operations in this place. file:///C|/Documents%20and%20Settings/Linda%20Grauer. ../How%20to%20Design%20Programs/curriculum-Z-H-26.html (1 of 8) [2/5/2008 4:50:15 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> ... <var> )) <exp> = <var> | <boo> | <sym> | <prm> | empty | ( <exp> <exp> ...<exp> ) | ( cond ( <exp> <exp> ) ... ( <exp> <exp> )) | ( cond ( <exp> <exp> ) ... ( else <exp> )) | ( local ( <def> ...<def> ) <exp> ) <var> = x | area-of-disk | circumference | ... <boo> = true | false <sym> = 'a | 'doll | 'sum | ... <num> = 1 | -1 | 3/5 | 1.22 | ... <prm> = + | - | cons | first | rest | ... Figure 56: Intermediate Student Scheme: The grammar Spelling out the problem suggests the necessary changes. First, we should include the names of functions and primitive operations in the definition of <exp> . Second, the first position in an application should allow things other than function names and primitive operations; at a minimum, it must allow variables that play the role of function parameters. In anticipation of other uses of functions, we agree on allowing expressions in that position. Here is a summary of the three changes:
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 Fall '07 term at WPI.

Page1 / 8

Section 20 - How to Design Programs An Introduction to Computing and Programming[Go to first previous next page contents index Section 20 Functions

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