Docsity

Exam I Sample Solution for Programming Language Concepts | 22C 111, Exams of Programming Languages

Toggle sidebar
bg1
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].
pf3

Partial preview of the text

Download Exam I Sample Solution for Programming Language Concepts | 22C 111 and more Exams Programming Languages in PDF only on Docsity!

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].

(b) The lower precedence operation ':' will appear earlier (i.e., above) function

application in the derivation tree. To introduce the ':' operation, we must derive

qop(r,5), and this in turn requires deriving rexp^5 , so this guides the first steps.

Specifically,

exp

exp

rexp

exp exp

exp

fexp

aexp

qvar

xs

qop

exp

fexp

fexp aexp

qvar

x

aexp

qvar

f

6 (r,5)^6