1.3.1 Procedures as Arguments
- View
- Revisions

Consider the following three procedures. The first computes the sum of the integers from a
through b
:
(define (sum-integers a b)
(if (> a b)
0
(+ a (sum-integers (+ a 1) b))))
The second computes the sum of the cubes of the integers in the given range:
(define (sum-cubes a b)
(if (> a b)
0
(+ (cube a) (sum-cubes (+ a 1) b))))
The third computes the sum of a sequence of terms in the series
which converges to π/8 (very slowly):[49]
(define (pi-sum a b)
(if (> a b)
0
(+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))
These three procedures clearly share a common underlying pattern. They are for the most part identical, differing only in the name of the procedure, the function of a
used to compute the term to be added, and the function that provides the next value of a
. We could generate each of the procedures by filling in slots in the same template:
(define ( a b)
(if (> a b)
0
(+ ( a)
( ( a) b))))
The presence of such a common pattern is strong evidence that there is a useful abstraction waiting to be brought to the surface. Indeed, mathematicians long ago identified the abstraction of summation of a series and invented “sigma notation,” for example
to express this concept. The power of sigma notation is that it allows mathematicians to deal with the concept of summation itself rather than only with particular sums — for example, to formulate general results about sums that are independent of the particular series being summed.
Similarly, as program designers, we would like our language to be powerful enough so that we can write a procedure that expresses the concept of summation itself rather than only procedures that compute particular sums. We can do so readily in our procedural language by taking the common template shown above and transforming the “slots” into formal parameters:
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
Notice that sum
takes as its arguments the lower and upper bounds a
and b
together with the procedures term
and next
. We can use sum
just as we would any procedure. For example, we can use it (along with a procedure inc
that increments its argument by 1) to define sum-cubes
:
(define (inc n) (+ n 1))
(define (sum-cubes a b)
(sum cube a inc b))
Using this, we can compute the sum of the cubes of the integers from 1 to 10:
(sum-cubes 1 10)
3025
With the aid of an identity procedure to compute the term, we can define sum-integers
in terms of sum
:
(define (identity x) x)
(define (sum-integers a b)
(sum identity a inc b))
Then we can add up the integers from 1 to 10:
(sum-integers 1 10)
55
We can also define pi-sum
in the same way:[50]
(define (pi-sum a b)
(define (pi-term x)
(/ 1.0 (* x (+ x 2))))
(define (pi-next x)
(+ x 4))
(sum pi-term a pi-next b))
Using these procedures, we can compute an approximation to π:
(* 8 (pi-sum 1 1000))
3.139592655589783
Once we have sum
, we can use it as a building block in formulating further concepts. For instance, the definite integral of a function f between the limits a and b can be approximated numerically using the formula
for small values of dx. We can express this directly as a procedure:
(define (integral f a b dx) (define (add-dx x) (+ x dx)) (* (sum f (+ a (/ dx 2.0)) add-dx b) dx)) (integral cube 0 1 0.01)
.24998750000000042(integral cube 0 1 0.001)
.249999875000001
(The exact value of the integral of cube
between 0 and 1 is 1/4.)
pi-next
and pi-term
within pi-sum
, since these procedures are unlikely to be useful for any other purpose. We will see how to get rid of them altogether in section 1.3.2. [back]
Exercises
Comments
Post new comment