0% found this document useful (0 votes)
11 views3 pages

Exercises 3.5 1, 3, 5, 9, 11

The document outlines exercises related to recursive functions and properties of lists, including defining a function to compute the length of a list, finding the maximum value in a list, proving that elements in a strictly increasing list are ordered, and implementing a function to flip an SList. It also includes proofs by induction for the properties of these functions. Each exercise is detailed with definitions, recursive cases, and proofs of correctness.

Uploaded by

hubercoello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Exercises 3.5 1, 3, 5, 9, 11

The document outlines exercises related to recursive functions and properties of lists, including defining a function to compute the length of a list, finding the maximum value in a list, proving that elements in a strictly increasing list are ordered, and implementing a function to flip an SList. It also includes proofs by induction for the properties of these functions. Each exercise is detailed with definitions, recursive cases, and proofs of correctness.

Uploaded by

hubercoello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Exercises 3.

5: 1, 3, 5, 9, 11
Thursday, November 11, 2021 8:38 PM

# 1. Let L be a list, as in Definition 3.3


Define a numerical function f as follows.
B. If L = x, a single element, then f (L) = 1.
R. If L = L , x for some list L , then f (L) = f (L ) + 1.
(a) Show the steps of a “top-down” computation, as in Example 3.28
to find the value of f (veni, vidi, vici).
(b) What does the value of f (L) tell you about the list L, in general?
(c) Prove your assertion in part (b), using induction.
a) f(veni, vidi, vici) = f(veni, vidi) + 1
= f(vini) + 1 + 1
=1+1+1
=3
b) The definition of f indicates the same as the value of f is 1 when the list contains one element and the
value of f for the list is 1 more than the value of f of the list with one less element.
f(L) is the length of the list L
c) P(n) = f(L) is the length of the list L with n elements
if L is a list with a single element then
f(L) = 1
so, P(1) is true
if we let P(k - 1) be true
F(L) is the length of the list L with k - 1 elements
L = list k with with length L(k)
L' = list of first k - 1 elements in L and x is the kth element in L.
L = L', x
length of L' = k - 1, since P(k) is true
f(L') = k - 1
step R:
f(L) = f(L', x) = F(L') + 1 = k - 1 + 1 = k
so, P(k) is true

# 3 Define a function max: R × R → R by

(a) Use this function to write a recursive function LMax(L) that returns the greatest value in L, where L is a list of numbers.
(b) Prove that your LMax function works. In other words, prove that LMax(L) returns the greatest value in the list L.
a) When the list L contains a single element x, then the greatest value is x and LMax(L) then needs to be equal to x.
LMax(L) = if L is a single element x
When the list L contains n >= 2 elements with the last element x and the remaining list L' , then the greatest value is the maximum of x and the greatest value in L'.
LMax(L) = Max(LMax(L'), x) if L = L',
Combining these two definitions, we then obtain the recursive function:
LMax(L) = { x , if L contains a single element x LMax(L) = { Max(LMax(L'), x), if L = L', x
b) P(n) is LMax(L) = greatest value in the list L with n elements.
LMax(L) = x
so P(1) is true
if we let P(k - 1) be true
LMax(L) =greatest value in L with k - 1 elements
L = list with K elements.
L' = list of first k - 1 elements in L x is the kth element in L
L = L', k
since P(k - 1) is true LMax(L') contains the greatest value in the list L'
LMax(L) = Max(LMax(L'),x)
LMax(L) is then the greatest value among LMax(L') and x. Since LMax(L') contains the greatest value in the list L', LMax(L) contains the greates value in the list L.
P(k) is true.

# 5. Prove that the elements of an SList are in strictly increasing order from left to right. That is, if x1x1, x2x2, x3x3, …, x2px2pare the
elements of the list, show that

(Use induction on p.)


P(n) = if x11, x22, x33,..., x2n2nare the elements on the Slist, then x11< x22< x33<... < x2n2n
Basic Step n = 0
let x11be an Slist. x11is in strictly increasing order from left to right.
so P(0) is true
inductive step: let P(k - 1) be true.
when x11, x22, x33,..., x2k−12k−1 are the elements in an Slist: x11< x22< x33<... < x2k−12k−1.
prove P(k) is true
let x11, x22, x33,..., x2k2kbe the elements of an Slist. this them implies that the Slist is if the form (X,Y) with x11, x22, x33,..., x2k−12k−1the elements of the Slist X and
x2k−12k−1+ 1, x2k−12k−1+2, x2k−12k−1+3,..., x2k2kthe elements of the Y list.
since P(k-1) is true, x11< x22< x33<... < x2k−12k−1and x2k−12k−1+ 1 < x2k−12k−1+ 2 < x2k−12k−1+ 3 <... <x2k2k.
step R (X,Y) can only be an Slist when the last element of X is less than the first element in Y, which implies 2k−12k−1< 2k−12k−1+ 1
x11< x22< x33<...< 2k−12k−1< 2k−12k−1+ 1 < x2k−12k−1+ 2 < x2k−12k−1+ 3 <... <x2k2k.
P(k) is true.

# 9. Let L be an SList. Define a recursive function Flip as follows.


B. Suppose L = x. Then Flip(L) = x.
R. Suppose L = (X, Y). Then Flip(L) = (Flip(Y), Flip(X)).
Compute Flip[((2,3), (7,9))], showing all steps.
Your Answer:
B. x is an Slist whn x is a real number
R. (X,Y) is an Slist when X and Y are Slists with the same number of elements and the last number in X is less than the first number in Y
B. Flip(x) = x
R. Flip((X,Y)) = (Flip((Y)), Flip((X)))
Flip[ ((2,3), (7.9))]
=(Flip((7,9)), Flip((2,3))) by R case
=(Flip(9), Flip(7)), (Flip(3), Flip(2))) by R case
=((9,7),(3,2)) by B case

# 11 Write a recursive function d(L) that returns the depth of an Slist L.

B. If L = x, then d(L) = 0.

R. If L = (X, Y), then d(L) = d(X) + 1.

You might also like