NOTE: The exam is on Thursday, May 10.
The exam will cover Chapters 9 - 12 from the textbook and the following material.
- Problem 1:
- Do the following Exercises from the textbook.
- From the end of Chapter 10, do Exercises 3, 4, and 5 (pages 163-164).
- From the end of Chapter 12, do Exercises 1, 2, 3, 7, and 8 (pages 203-204).
- Be sure that you are familiar with the exercises that you turned in for Chapters 9 and 11.
- Problem 2:
- The following function implements a simple mathematical function that might come up in any algebra or calculus course. Write this mathematical function using standard mathematical notation. Explain in detail how this ML function implements the mathematical function.
fun f (x,y) = (if y < 0 then (op -) else (op +))(x,y);
- Problem 3:
- In the following expression, draw an arrow from each variable reference to its associated declaration as a formal parameter.
fn x => fn y => (fn x => (x y)) x;
- Problem 4:
- What is the ML type for each of the following two anonymous functions? Explain your reasoning.
fn x => fn y => (x y);
fn x => fn y => (y x);
- Problem 5:
- If two functions
f and g are both functions of a single argument such that the values produced by g are legal arguments to f , then the composition of f and g is defined to be the function that first applies g to its argument and then applies f to the result. Write an ML function called compose that takes any two one-argument functions and returns the function that is their composition. For example,
((compose sqrt abs) -4.0) ==> 2.0
The compose function has the type
('b -> 'c) -> ('a -> 'b) -> 'a -> 'c
- Problem 6:
- Consider the following definitions.
fun makeScaled scale f =
fn x => scale * (f x);
fun addOne x = 1 + x;
val mystery = makeScaled 3 addOne;
For each of the following questions, be sure to explain how you arrived at your answer.
(a) What is the value of the expression (mystery 4) ?
(b) What is the value of the expression ((makeScaled 2 (makeScaled 3 addOne)) 4) ?
- Problem 7:
- In the following C program there are two assignments to the entity
**x . For each of these assignments, draw a picture that would help someone visualize the state, at that point in the program, of all of the variables and their memory locations being used by this program. Also, what does this program print?
#include <stdio.h>
int main()
{ int **x;
int *y;
int z;
x = (int**) malloc(sizeof(int*));
y = (int*) malloc(sizeof(int));
z = 1;
*y = 2;
*x = y;
**x = z;
printf("%d\n", *y);
z = 3;
printf("%d\n", *y);
**x = 4;
printf("%d\n", z);
return 0;
}
- Problem 8:
- Consider the following outline of a program written in a block structured language that uses dynamic scoping.
program main;
var x, y, z;
procedure sub1;
var a, y, z;
begin
// some code
end;
procedure sub2;
var a, b, z;
begin
// some code
end;
procedure sub3;
var a, x, w;
begin
// some code
end;
begin // main
// some code
end.
(a) Suppose that main calls sub1 , sub1 calls sub2 , and sub2 calls sub3 . List all of the variables that are visible in the activation of sub3 and specify where each visible variable was declared.
(b) Suppose that main calls sub2 , sub2 calls sub3 , and sub3 calls sub1 . List all of the variables that are visible in the activation of sub1 and specify where each visible variable was declared.
(c) Suppose that main calls sub3 and sub3 calls sub1 . List all of the variables that are visible in the activation of sub1 and specify where each visible variable was declared.
- Problem 9:
- Suppose that we have functions nested in the following manner.
f1
/ | \
/ | \
f2 f3 f4
/ \ |
/ \ |
f5 f6 f7
| / | \
| / | \
f8 f9 f10 f11
|
|
f12
Sketch a picture of the linked list of activation records, including nesting links, when function f1 calls f2 which calls f5 and then function f5 calls f1 and passes to f1 a reference to f6 and then f1 calls f4 and passes to it its reference to f6, and then f4 calls f7 and passes to it the reference to f6, and then f7 calls f6.
|