22C:111 -- Fall 2005
page 1 of 3
Exam I
Sample Solutions
Problem 1.
(a) X is the correct answer -- the number of choices of 0 {110} 1 corresponds to the
number of times around the outer-loop of diagram A; for each 0 {110} 1 choice, the
number of choices of 110 corresponds to the number of times around the inner-loop on
that pass of the outer loop. For instance, W is wrong because it does not include 0101
while A does.
(b) Z is the correct answer -- e agrees with B by the "straight through" path, 0{1}0Z
agrees for the 1-subloop possibly followed by another outer-loop iteration, and 0Z0Z
agrees for the B-recursion for the first Z and another possible outer-loop iteration for
the second. For instance, Y is wrong because it allows 0100 while B does not.
Problem 2.
Function application has higher precedence than list construction (:) so both the typing
and derivation structure should indicate that f is applied only to x, and the result of the
expression is a list with first element f x and tail xs. Note that the "types" referred to are
either concrete (Int, Bool, etc.) or polymorphic (a, b, etc.) types.
(a) f x is function application and since there are no pre-defined types involved, f is
polymorphic with f :: a -> b, x :: a, and f x :: b. From its use with ':', xs is a list whose
element type is undetermined, so xs :: [c]. For f x : xs to be type correct, it must be
that c = b. Hence the types are f :: a -> b, x :: a, and xs :: [b].