CSE 505 Midterm
November 4, 1994
50 minutes, open notes, 110 points total
- (24 points) Suppose the following Miranda script has been filed in.
weird * ::= Nothing | I num | B bool | Anything *
tree * ::= EmptyTree | Node * (tree *) (tree *)
my_const k x = k
my_map f [] = []
my_map f (x:xs) = f x : my_map f xs
double x = 2*x
tree_map f EmptyTree = EmptyTree
tree_map f (Node x left right) = Node (f x) (tree_map f left) (tree_map f right)
What is the result of evaluting the following Miranda expressions? If
there is a compile-time type error, or a non-terminating computation, say
so. If the result is an infinite data structure, give some initial parts
of it. If the expression is followed by ::, then give the type instead of
the value.
my_map double [1..]
my_map double ::
my_map (my_const False) [] ::
my_const ::
I 3 ::
Anything 3 ::
[I 3, B False, Anything [3], Nothing, Anything 4] ::
tree_map double ::
tree_map double (Node 10 (Node 20 EmptyTree EmptyTree) EmptyTree)
my_const (Node 10 (Node True EmptyTree EmptyTree)) ::
my_const 3 (1/0)
my_const (1/0) 3
- (10 points) Consider the following code in an Ada-like language.
type aa is array(1..10) of integer;
type bb is array(1..10) of integer;
w: aa;
x,y: bb;
z: array(1..10) of integer;
...
x := w;
y := x;
z := w;
Which of the assignments would be legal if the language determined type
equivalence using pure name equivalence? Which would be legal if the
language determined type equivalence using structual equivalence?
- (10 points) The combinators S, K, and I are defined as follows:
S f g x = f x (g x)
K x y = x
I x = x
In fact we only need S and K, since SKK=I. Prove this equivalence.
- (10 points) Consider the following program in an Algol-like language.
begin
integer n;
procedure p(j: integer);
begin
print(j,n);
n := n+j;
print(j,n);
n := n+j;
print(j,n);
end procedure p;
n := 5;
p(n+2);
print(n);
end;
What is the output when j is:
- passed by name?
- passed by value?
- (10 points) Consider the following program in an Algol-like language.
begin
integer n;
procedure p(j, k: integer);
begin
n := n+j+k;
print(j,k,n);
end procedure p;
n := 2;
p(n,n);
end;
What is the output when j and k are both:
- passed by value?
- passed by value result?
- passed by reference?
In each case, if any variables are aliased, say so.
- (10 points) Consider the following Simula program:
begin
class a;
begin
end a;
a class b;
begin
end b;
b class c;
begin
end c;
ref(a) va;
ref(b) vb;
ref(c) vc;
va :- new a;
vb :- va;
vc :- new c;
vb :- vc;
vc :- va qua c;
In other words, class b is a subclass of a, and class c is a subclass of b.
Which of the assignments will pass the compiler's type check, and which
will fail? (Just write "yes" or "no" next to each assignment.)
-
(10 points)
Suppose that B1 and B2 have been declared as global variables in
Smalltalk-80. One has the following code in a workspace in Smalltalk-80,
and selects it all and then "do it" from the menu.
| x y |
x := 5.
y := 6.
B1 := [:temp | y := temp. x+y].
x := 0.
Subsequently, one selects the following code and says "print it". What
is printed? Explain your reasoning.
| x y |
x := 12.
y := 15.
B2 := [:temp | y := temp. x+y].
(B1 value: 3) + (B2 value: 4)
- (26 points) Tacky but easy-to-grade true/false questions!
- Every piece of data in Simula is an object.
- Every piece of data in Smalltalk-80 is an object.
- Smalltalk-80 is statically typed.
- Smalltalk-80 is type safe.
- Ada is statically typed.
- Ada is type safe.
- Although Simula was a major inspiration for later languages that
support abstract data types, classes in Simula don't hide their internal
representations from outside clients.
- The combinator graph reduction implementation used in Miranda has
certain self-optimizing properties. For example, given a function
definition such as
f x = (4+5)*x
when the function is first invoked, 4+5 will be evaluated, and thereafter
the function will just compute 9*x when it is invoked.
- Fortran is weakly typed. As an example of a type loophole, one can
use the
equivalence
statement to direct the compiler to store a real
and an integer in the same storage location, so that the real number can be
accessed incorrectly as an integer and vice versa.
- Algol-60 is statically typed.
- In Smalltalk-80, control structures are implemented using ordinary
message sending, often with blocks as arguments.
- Assignment in Smalltalk is written as the infix operator
:=
and is a message to class Variable
.
- Since there are no reserved words in Fortran,
IF=5
is
a legal assignment statement.
- Which do you think is the most appropriate name for the
temporary building next to Sieg?
- The Chateau
- The Trailer Court
- The Projects
- Lazowskaville
- Other [what?]