How to Design Programs: An Introduction to Programming and Computing

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

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 (1 of 15) [2/5/2008 4:45:50 PM]
Image of page 1

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