Professional Documents
Culture Documents
Midterm 2
Midterm 2
Name:
Instructions
Please answer each question in the space provided.
The midterm needs to be turned in via ICON.
Be sure to review the syllabus for details about this course’s cheating policy.
1. (7 points) Provide a short answer (1-3 sentences) to each of the following questions.
1. Describe one advantage and one disadvantage of lazy evaluation versus eager evaluation
in programming languages.
3. Explain the input/output functionality of a program like FSLex (i.e., what it takes as
input and what it produces as output).
1. Provide a simplified typing rule based on the assumption that the language has dynamic
scope.
3. (6 points) Consider the following polymorphic tree data type and combinator
type ’a btree =
| Leaf
| Node of ’a btree * ’a * ’a btree
Page 2
Using treeFold and auxiliary non-recursive functions:
1. f : ’a btree -> int, which takes a btree and returns the depth of the tree, in which
a Leaf tree has depth 0 and a Node(t1, n, t2) tree has depth 1 plus the maximum of
the depths of t1 and t2. Ex: Node (Leaf,3,Node (Leaf,4,Leaf)) returns 2.
2. f : int btree -> string, which takes a btree and returns a string containing its node
values in inorder, i.e. left subtree, then root, then right subtree. The values must be
separated by semicolons, with no repeated occurrences of semicolons. The string must
start and finish with node values.
Ex: Node (Node (Leaf,3,Node (Leaf,4,Leaf)),2,Leaf) returns "3;4;2"
Page 3
4. (8 points) Given the task to extend the micro-C language to implement conditional expressions
of the form e1 ? e2 : e3, describe, for each of the following, which additions would you
make, at which point, and why:
Page 4
Page 5