Comprehensive
Exam:
Programming Languages Autumn
2002
1.
(7
points) Type Inference
(a)
(3
points) This part of the question asks you to write the ML type of an ML function.
Recall that an ML function that takes two integer arguments and returns a Boolean
value has type
(int
*
i n t )
+
bool.
What is the ML type for the following function?
fun applytwice(f
,
x)
=
f (f (x))
;
Answer:
('a
>
'a)
*
'a
>
'a
(b)
(2
points) Describe types
<typel>, <type2>
and
<type3>
that will make the analo
gous
C
code below statically typed. You may write the types using correct or mostly
correct
C
syntax. In particular you can write
<type2>
f
in a form where
f
appears
in the middle of the type, if you prefer (and if this is correct).
<typel> applytwice(<type2> f , <type3> x)
(
return f (f
(x)
)
;
J;
Answer:
One possibility is
<typeI>=<type3>=int
and
<type2>=
int
(*)
(int).
Some prefer to write
i n t
(*
f
)
(int)
(c) (2 points) Why is the
ML
type more useful than the
C
type for
applytwice?
Specif
ically, what flexibility does the ML type system give, for functions with the kind of
type you wrote in (a), that the
C
type system does not give you for the function in
(b)
?
Answer:
The ML function is polymorphic, which means that you can apply
applytwice
to many types of arguments. In contrast, the
C
function can only be declared and
used for one specific choice of types.
2.
(5
points)
ALGOL
60
PassByName
In passbyname, the result of a procedure call is the same as if the formal parameters
were substituted into the body of the procedure. This rule for defining the result of
a procedure call by copying the procedure and substituting for the formal parameters is
called the
Algol
60
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 '09
 main data structure

Click to edit the document details