Answers to Self-Study Questions
Test Yourself #1
Question 1
let rec member(x, L) =
case (L) {
nil: false
y::Tail: if (x==y)
then true
else member(x, Tail)
}
Question 2
let rec union(L1, L2) =
case (L1) {
nil: L2
x::Tail: if (member(x,L2))
then union(Tail, L2)
else x::union(Tail, L2)
}
Test Yourself #2
Question 1
let eqzero(x, b) = b || x==0 in
let has0(L) = reduce(L, eqzero, false)
Question 2
let eq(x) = lambda((y,b), b || x == y) in
let member(x, L) = reduce(L, eq(x), false)
Test Yourself #3
sumprimes(3) = sum(3, sieve(from(2)))
= sum(3, sieve(2::from(3)))
= sum(3, 2::sieve(filter(2, from(3))))
= 2 + sum(2, sieve(filter(2, from(3))))
= 2 + sum(2, sieve(filter(2, 3::from(4))))
= 2 + sum(2, sieve(3::filter(3, filter(2, from(4)))))
= 2 + 3 + sum(1, sieve(filter(3, filter(2, from(4)))))
= 2 + 3 + sum(1, sieve(filter(3, filter(2, 4::from(5)))))
= 2 + 3 + sum(1, sieve(filter(3, filter(2, from(5)))))
= 2 + 3 + sum(1, sieve(filter(3, filter(2, 5::from(6)))))
= 2 + 3 + sum(1, sieve(filter(3, 5::filter(2, from(6)))))
= 2 + 3 + sum(1, sieve(5::filter(3, filter(2, from(6)))))
= 2 + 3 + sum(1, 5::sieve(filter(3, filter(2, from(6)))))
= 2 + 3 + 5 + sum(0, sieve(filter(3, filter(2, from(6)))))
= 2 + 3 + 5 + 0
= 10
Test Yourself #4
reverse(concat(L1, L2)) = concat(reverse(L2, reverse(L1)))
Base Case : L1 = nil
LHS = reverse(concat(nil,L2))
= reverse(L2) //def of concat
RHS = concat(reverse(L2), reverse(nil))
= concat(reverse(L2), nil) //def of reverse
= reverse(L2) //def of concat
Inductive Step
assume: ∀ L1 of length ≤ n, reverse(concat(L1, L2)) = concat(reverse(L2), reverse(L1))
show: true for L1 of length n+1, i.e., L1 = x::Tail
LHS = reverse(concat(x::Tail, L2))
= reverse(x::concat(Tail, L2)) //def of concat
= concat(reverse(concat(Tail, L2)), x::NIL) //def of reverse
RHS = concat(reverse(L2), reverse(x::Tail))
= concat(reverse(L2), concat(reverse(Tail), x::nil)) //def of reverse
= concat(concat(reverse(L2), reverse(Tail)), x::nil) //concat is associative
= concat(reverse(concat(Tail, L2)), x::nil) //inductive hypothesis