Section 23

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 23 Mathematical Examples Applying mathematics to real-world problems requires programs that implement mathematical functions. In many cases, the programs also employ functions that consume and produce functions. Mathematics is therefore a great starting point for practicing programming with functions and, more generally, for creating abstract functions. The first subsection covers sequences and series, a key element of mathematics. The second section discusses integration, which relies heavily on series. Finally, the third section introduces function differentiation. 23.1 Sequences and Series In pre-algebra and algebra, we encounter sequences (also known as progressions) of numbers. Here are three examples: 1. 0, 2, 4, 6, 8; 2. 1, 3, 5, 7, 9; 3. 5, 10, 15, 20, 25. The first two enumerate the first five even and odd natural numbers, respectively; the last one lists the first five positive integers, evenly divisible by 5. Sequences can also be infinite: 1. 0, 2, 4, 6, 8, ... ; 2. 1, 3, 5, 7, 9, ... ; 3. 5, 10, 15, 20, 25, ... Following mathematical tradition, infinite sequences end in `` ... '' and the reader must determine how to find more of the terms in the sequence. One way to understand sequences of numbers, especially infinite ones, is to match them up with an enumeration of the natural numbers. For example, the even and odd (natural) numbers match up like this: [curriculum3a-Z-G-2.gif] It is easy to see from this table that every even number is 2 · i for its index i and that an odd number is 2 · i + 1. Both statements can be translated into simple Scheme functions: file:///C|/Documents%20and%20Settings/Linda%20Graue. ..How%20to%20Design%20Programs/curriculum-Z-H-29.html (1 of 13) [2/5/2008 4:51:10 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 ;; make-even : N -> N [even] ;; to compute the i-th even number (define (make-even i) (* 2 i)) ;; make-odd : N -> N [odd] ;; to compute the i-th odd number (define (make-odd i) (+ (* 2 i) 1)) In short, functions from natural numbers to numbers are representations of infinite sequences. A mathematical series is the sum of a sequence. The three finite sequences have the sums 20, 25, and 75, respectively. In the case of infinite sequences it is often interesting to consider a finite portion, staring with the first one. 51 For example, adding the first 10 even numbers yields 90, and adding the first 10 odd numbers yields 100. Computing a series is clearly a job for a computer. Here are functions that add up the first n odd or even numbers, respectively, using make-even and make-odd to compute the required numbers: ;; series-even : N -> number ;; to sum up the first ;; n even numbers (define (series-even n) (cond [(= n 0) (make-even n)] [else (+ (make-even n) (series-even (- n 1)))])) ;; series-odd : N -> number ;; to sum up the first ;; n odd numbers (define (series-odd n)
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 Fall '07 term at WPI.

Page1 / 13

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