Section 24

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 24 Intermezzo 4: Defining Functions on the Fly Many uses of abstract functions require the definition of auxiliary functions. Consider filter1 , which consumes a filtering function, a list, and a filtering item. In the previous section alone, we encountered three uses of filter1 with three different auxiliary functions: squared? , < ir , and eq-ir? . Since these auxiliary functions are only used as arguments to filter1 , we should employ the program organization guidelines from the preceding intermezzo ( 18 ). That is, we should use a local -expression to indicate that the application of filter1 and the auxiliary function definition belong together. Here is one possible arrangement for the filter1-eq-ir? combination: ;; find : list-of-IRs symbol -> boolean (define (find aloir t) (local ((define (eq-ir? ir p) (symbol=? (ir-name ir) p))) (filter1 eq-ir? aloir t))) An alternative arrangement places the local -expression where the function is needed: ;; find : list-of-IRs symbol -> boolean (define (find aloir t) (filter1 (local ((define (eq-ir? ir p) (symbol=? (ir-name ir) p))) eq-ir?) aloir t)) This alternative is feasible because the names of functions -- like eq-ir? -- are now legitimate expressions and can play the role of local 's body. Thus the local -expression introduces a function definition and returns the function as its result. Because good programmers use abstract functions and organize their programs in a tidy manner, it is not surprising that Scheme provides a short-hand for this particular, frequent use of local . The short-hand is called a lambda -expression and greatly facilitates the introduction of functions like eq-ir? , file:///C|/Documents%20and%20Settings/Linda%20Grauer. ../How%20to%20Design%20Programs/curriculum-Z-H-30.html (1 of 7) [2/5/2008 4:51:23 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 squared? , or < ir . The following two subsections introduce the syntax and semantics of lambda - expressions. The last subsection discusses its pragmatics. Syntax of lambda
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 / 7

Section 24 - 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