Anda di halaman 1dari 2

hi

Question 1.5

(a) The declaration typechecks. The result of a recursive call to the function is multiplied by an
integer (0), resulting in an integer, matching the declaration. The name of the identifier is f having
type int → int. The value is bound to the lambda expression fn (x:int) => 0 * f(x mod 10).

(b) The expression typechecks. Its type is int, but its value is indeterminate since the function
repeatedly makes recursive calls to itself, but since there is no base case, evaluation never terminates.

Question 2.1

(a) It is bound to 13. The most recent declaration of x in the scope is on line 5, so the value is 13.

(b) It is bound to 93.6. The most recent declaration of p in the scope is on line 7, which has a
value of 93.6 when evaluated.

(c) It is bound to the value of the parameter passed to the function, which is the most recent
declaration. It is NOT bound to 100, because the declaration at line 8 is not in the scope.

(d) The value returned by the function is (53, 93.6), which is of type int × real. The second value
of the tuple, q, is computed from the value of p on line 10 (part b), and the first value of the tuple
is a computation involving q and the parameter of the function, x = 41.

Question 2.2

The value of m is 9. It is computed with modmult(k, 4) where k is bound to 7. However, at the


time of function declaration, k was bound to 19, so this is the value used inside modmult. Thus,
the result is m = (7 × 4) mod 19 = 9.

Question 3.2

Theorem:
For every value k ≥ 0 and for every function f , sumodd(k, f ) = f (1) + f (3) + · · · + f (2k − 1).
Proof:
By mathematical induction on k.
Base case:
k = 0: We need to show that sumodd(0, f ) = 0, which is the sum on the first zero odd integers. We
see that this is true by examining the code. Thus, the base case holds.
Inductive hypothesis:
Suppose that for some k 0 ≥ 0, sumodd(k 0 , f ) = f (1) + f (3) + · · · + f (2k 0 − 1).
Inductive step:
We want to show that sumodd(k 0 + 1, f ) = f (1) + f (3) + · · · + f (2(k 0 + 1) − 1). We can evaluate

1
hi

sumodd(k 0 + 1, f ) by examining the code:

sumodd(k 0 + 1, f ) = f (2(k 0 + 1) − 1) + sumodd(k 0 , f )

Using the inductive hypothesis, we can substitute the value of sumodd(k 0 , f ):

f (2(k 0 + 1) − 1) + sumodd(k 0 , f ) = f (2(k 0 + 1) − 1) + f (1) + f (3) + · · · + f (2k 0 − 1)


= f (1) + f (3) + · · · + f (2k 0 − 1) + f (2(k 0 + 1) − 1)

This is the sum of the first k 0 + 1 odd terms of f , which is what we wanted to show. Thus, the
function correctly calculates the value of sumodd as described in the specification. 

Question 3.3

In order to sum over the first k even integers, we can sum over the first k odd integers using sumodd
if each value of the function f (n) corresponds to f (n + 1). To accomplish this, we can use an
anonymous wrapper function to call the original function with parameter n + 1. In the example
given in the handout, to sum the first 5 odd terms of f (n) = n2 , we use:

sumodd(5, fn (n:int) => n*n)

To instead sum the first 5 even terms, we can use:

sumodd(5, fn (n:int) => (fn (n:int) => n*n)(n+1))

Question 3.4

In order to sum over a range of odd integers from k1 to k2 , we must first convert these numbers
to indexes used in the sumodd function. Write c1 = k12+1 and c2 = k22+1 . We now want to find the
sum over the c1 th odd integer to the c2 th odd integer of f. Rewrite c1 − 1 = k12−1 . We know that
sumodd(c1 − 1, f ) = f (1) + · · · + f (k1 − 2) and sumodd(c2 , f ) = f (1) + · · · + f (k2 ). Thus, if we
subtract the first expression from the second, we obtain the sum f (k1 ) + · · · + f (k2 ) that we want.
In terms of k1 and k2 , this can be written as:

sumodd((k2+1) div 2, f) - sumodd((k1-1) div 2, f)