06.03.2017 Views

Mathematics for Computer Science

e9ck2Ar

e9ck2Ar

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

“mcs” — 2017/3/3 — 11:21 — page 209 — #217<br />

7.2. Strings of Matched Brackets 209<br />

Now,<br />

[ ] [ ] D [ ] [ ] 2 RecMatch (letting s D ; t D [ ] )<br />

[ [ ] ] D [ [ ] ] 2 RecMatch (letting s D [ ] ; t D )<br />

[ [ ] ] [ ] 2 RecMatch (letting s D [ ] ; t D [ ] )<br />

are also strings in RecMatch by repeated applications of the constructor case; and<br />

so on.<br />

It’s pretty obvious that in order <strong>for</strong> brackets to match, there had better be an equal<br />

number of left and right ones. For further practice, let’s carefully prove this from<br />

the recursive definitions, beginning with a recursive definition of the number # c .s/<br />

of occurrences of the character c 2 A in a string s:<br />

Definition 7.2.2.<br />

Base case: # c ./ WWD 0.<br />

Constructor case:<br />

# c .ha; si/ WWD<br />

(<br />

# c .s/ if a ¤ c;<br />

1 C # c .s/ if a D c:<br />

The following Lemma follows directly by structural induction on Definition 7.2.2.<br />

We’ll leave the proof <strong>for</strong> practice (Problem 7.9).<br />

Lemma 7.2.3.<br />

# c .s t/ D # c .s/ C # c .t/:<br />

Lemma. Every string in RecMatch has an equal number of left and right brackets.<br />

Proof. The proof is by structural induction with induction hypothesis<br />

h<br />

i<br />

P.s/ WWD # [ .s/ D # ] .s/ :<br />

Base case: P./ holds because<br />

# [ ./ D 0 D # ] ./<br />

by the base case of Definition 7.2.2 of # c ./.

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

Saved successfully!

Ooh no, something went wrong!