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 11
Natural Numbers
The only selfreferential data definitions we have seen thus far involved
cons
and lists of arbitrary
length. We needed such data definitions because the classes of lists that we wanted to process were of
arbitrary size. Natural numbers are another class of data whose elements are of arbitrary size; after all,
there is no limit on how large a natural number can be, and, at least in principle, a function should be
able to process them all.
In this section, we study how to describe natural numbers with selfreferential data definitions and how
to develop functions that process natural numbers in a systematic fashion. Since such functions come in
many flavors, we study several different flavors of definitions.
11.1
Defining Natural Numbers
People normally introduce natural numbers via enumeration:
0
,
1
,
2
, etc.
34
The abbreviation ``etc.'' at
the end says that the series continues in this manner. Mathematicians and mathematics teachers often use
dots for the same purpose. For us, however, neither the ``etc.'' nor the dots is good enough, if we wish to
design functions on natural numbers systematically. So, the question is what it means to write down
``etc.,'' or put differently, what a complete, selfcontained description of the natural numbers is.
The only way to remove the informal ``etc.'' from the enumeration is to describe the collection of
numbers with a selfreferential description. Here is a first attempt:
0 is a natural number.
If
n
is a natural number, then one more than
n
is one, too.
While this description is still not quite rigorous,
35
it is a good starting point for a Schemestyle data
description:
A
naturalnumber
is either
1.
0
or
file:///C/Documents%20and%20Settings/Linda%20Graue.
..How%20to%20Design%20Programs/curriculumZH15.html (1 of 16) [2/5/2008 4:46:07 PM]
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document How to Design Programs: An Introduction to Computing and Programming
2.
(add1 n)
if
n
is a natural number.
The operation
add1
adds
1
to a natural number. Of course, we could use
(+ .
.. 1)
but
add1
stands
out and signals ``natural number,'' as opposed to arbitrary number, to the reader of a data definition and a
function.
Although we are familiar with natural numbers from school, it is instructive to construct examples from
the data definition. Clearly,
0
is the first natural number, so
(add1 0)
is the next one. From here, it is easy to see the pattern:
(add1 (add1 0))
(add1 (add1 (add1 0)))
(add1 (add1 (add1 (add1 0))))
The examples should remind us of the lists construction process. We built lists by starting with
empty
and by
cons
tructing on more items. Now we build natural natural numbers by starting with
0
and by
add
ing on
1
. In addition, natural numbers come with centuryold abbreviations. For example,
(add1
0)
is abbreviated as
1
,
(add1 (add1 0))
as
2
, and so on.
A function on natural numbers must extract the number that went into the construction of a positive
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 Spring '07 term at WPI.
 Spring '07
 Fisler

Click to edit the document details