06.03.2017 Views

Mathematics for Computer Science

e9ck2Ar

e9ck2Ar

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

“mcs” — 2017/3/3 — 11:21 — page 211 — #219<br />

7.3. Recursive Functions on Nonnegative Integers 211<br />

This definition may seem ok, but it isn’t: f ./ winds up with two values, and<br />

consequently:<br />

0 D f ./ (f base case))<br />

D f . /<br />

D 1 C f ./ C f ./<br />

D 1 C 0 C 0 D 1<br />

This is definitely not a situation we want to be in!<br />

(concat def, base case)<br />

(f concat case);<br />

(f base case):<br />

7.3 Recursive Functions on Nonnegative Integers<br />

The nonnegative integers can be understood as a recursive data type.<br />

Definition 7.3.1. The set N is a data type defined recursively as:<br />

0 2 N.<br />

If n 2 N, then the successor n C 1 of n is in N.<br />

The point here is to make it clear that ordinary induction is simply the special<br />

case of structural induction on the recursive Definition 7.3.1. This also justifies the<br />

familiar recursive definitions of functions on the nonnegative integers.<br />

7.3.1 Some Standard Recursive Functions on N<br />

Example 7.3.2. The factorial function. This function is often written “nŠ.” You will<br />

see a lot of it in later chapters. Here, we’ll use the notation fac.n/:<br />

fac.0/ WWD 1.<br />

fac.n C 1/ WWD .n C 1/ fac.n/ <strong>for</strong> n 0.<br />

Example 7.3.3. Summation notation. Let “S.n/” abbreviate the expression “ P n<br />

iD1 f .i/.”<br />

We can recursively define S.n/ with the rules<br />

S.0/ WWD 0.<br />

S.n C 1/ WWD f .n C 1/ C S.n/ <strong>for</strong> n 0.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!