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]

This
** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*