Exercises 3.5 1, 3, 5, 9, 11
Exercises 3.5 1, 3, 5, 9, 11
5: 1, 3, 5, 9, 11
Thursday, November 11, 2021 8:38 PM
(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
B. If L = x, then d(L) = 0.