# notes10 - In the example about areas, we are generalizing a...

This preview shows page 1. Sign up to view the full content.

In the example about areas, we are generalizing a pattern by using a variable number instead of a constant number. But we can also generalize a pattern in which it’s a function that we want to be able to vary: ;;;;; In file cs61a/lectures/1.3/general.scm (define (sumsquare a b) (if (> a b) 0 (+ (* a a) (sumsquare (+ a 1) b)) )) (define (sumcube a b) (if (> a b) 0 (+ (* a a a) (sumcube (+ a 1) b)) )) Each of these functions computes the sum of a series. For example, (sumsquare 5 8) computes 5 2 + 6 2 + 7 2 + 8 2 . The process of computing each individual term, and of adding the terms together, and of knowing where to stop, are the same whether we are adding squares of numbers or cubes of numbers. The only di±erence is in deciding which function of a to compute for each term. We can generalize this pattern by making the function be an additional argument, just as the shape number was an additional argument to the area function: (define (sum fn a b) (if (> a b) 0 (+ (fn a) (sum fn (+ a 1) b)) ))
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/17/2010 for the course COMPUTER S 26275 taught by Professor Harvey,b during the Spring '10 term at Berkeley.

Ask a homework question - tutors are online