Section 10

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 10 More on Processing Lists The functions in section 9 consume lists that contain atomic data, especially numbers, symbols, and booleans. But functions must also be able to produce such lists. Furthermore, they must be able to consume and produce lists that contain structures. We discuss these cases in this section, and we continue practicing the use of the design recipe. 10.1 Functions that Produce Lists Recall the function wage from section 2.3 : ;; wage : number -> number ;; to compute the total wage (at $12 per hour) ;; of someone who worked for h hours (define (wage h) (* 12 h)) The wage function consumes the number of hours some employee worked and produces the weekly wage payment. For simplicity, we assume that all employees earn the same hourly rate, namely, $12. A company, however, isn't interested in a function like wage , which computes the wage of a single employee. Instead, it wants a function that computes the wages for all of its employees, especially if there are a lot of them. Call this new function hours -> wages . It consumes a list that represents how many hours the employees of the company worked and must produce a list of the weekly wages they earned. We can represent both the input and the output as Scheme lists of numbers. Since we already have a data definition for the inputs and outputs, we can immediately start our function development: ;; hours -> wages : list-of-numbers -> list-of-numbers ;; to create a list of weekly wages from a list of weekly hours (alon) (define ( hours -> wages alon) . ..) Next we need some examples of inputs and the corresponding outputs: empty file:///C|/Documents%20and%20Settings/Linda%20Graue. ..How%20to%20Design%20Programs/curriculum-Z-H-14.html (1 of 15) [2/5/2008 4:45:50 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 (cons 28 empty) (cons 40 (cons 28 empty)) empty (cons 336 empty) (cons 480 (cons 336 empty)) The outputs are obtained by calculating the wage for each item on the list to the left. Given that hours -> wages consumes the same class of data as, say, the function sum , and given that the shape of a function template depends only on the shape of the data definition, we can reuse the list-of- numbers template: (define ( hours -> wages alon) (cond [(empty? alon) . ..] [else . .. (first alon) . .. ( hours -> wages (rest alon)) . ..])) Starting with this template, we can turn to the most creative step of function development: the definition of the function body. Following our recipe, we consider each cond -line in isolation, starting with the simpler case. First, assume (empty? alon) is true, which means that the input is empty . The answer in this case is empty : (define ( hours -> wages alon) (cond [(empty? alon) empty] [else . .. (first alon) . .. ( hours -> wages (rest alon)) . ..])) Second, assume that alon was cons tructed from a number and a list of numbers. The expressions in the
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 / 15

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