Exam 2 Review Problems for CS 31600, Spring 2023
The exam will be Thursday, May 4.
The exam will be over the following topics.
1) Global environment (Language_2).
2) Lexical scope (nested blocks, local environments, Language_3).
3) Function environments (activation environment, Language_5).
4) Static analysis vs. evaluation.
5) Lambda expressions (Languange_5 and Language_6).
6) Anonymous functions (JavaScript and Language_6).
Each language zip file contains a file called Language_n.txt (for n = 1 to 6).
Those text files are the reading material for the above topics.
Here are some review/practice problems for the exam.
Problem 1: Assuming that the following code is in a block structured
language like C or C++, what would this code print out?
{
int x = 1;
int y = 2;
{
int x = 3;
println(x+y);
x = 5;
y = 6;
}
{
println(x);
println(y);
int x = 7;
int y = 8;
}
println(x);
println(y);
}
Problem 2: Here is a program written in Language_5.
(prog
(var c 5)
(fun f
(lambda x
(+ (* x x) c))) // Draw Picture Here.
(fun g
(lambda u v
(+ (apply f u) (* v v v) c)))
(begin
(var a 4)
(var b 5)
(begin
(var r 0)
(var s 1)
(begin
(var s a)
(var t (apply g s b))
(print t)
}
)
)
)
Draw a picture of the six environment objects that exist
when the program gets to the designated line of code. Your
picture should show what variable bindings are in each
environment object and how the objects are linked together.
Also, label each env object as a global env, a local env,
or a function activation env.
For an example of an environment picture, see the file
Language-5\function scopes\environment-picture-1.txt
in the zip file Language-5.zip
Problem 3: Here is a program written in Language_5.
(prog
(var c 1)
(fun g (lambda x
(+ x c)) // Draw Picture Here.
)
(fun f
(lambda x
(begin
(var u 2)
(var v 3)
(apply g (+ u v x))
)
)
)
(begin
(var a 4)
(var b 5)
(print (apply f (* a b)))
)
)
Draw a picture of the five environment objects that exist
when the program gets to the designated line of code. Your
picture should show what variable bindings are in each
environment object and how the objects are linked together.
Also, label each env object as a global env, a local env,
or a function activation env.
Problem 4: Here is the AST for a program in Language_4.
Write the text of the program in Language_4. As always,
your code should be nicely formatted and indented.
prog
/ \
/ \
var while
/ \ / \
/ \ / \
x 0 < begin
/ \ / \
/ \ / \
x 5 set print
/ \ \
/ \ \
x + *
/ \ / \
/ \ / \
x 1 x 2
Problem 5: Draw the AST for this Language_5 program.
(prog
(var w 10)
(fun f (lambda x (var u (* x x w))))
(begin
(var x 0)
(begin
(var y 2)
(begin
(set x w)
(var z (apply f (+ x y w)))))))
Problem 6: Here is a simple JavaScript function.
function f(a, b) {
return 3 * a + b * b;
}
Rewrite this function using let and the JavaScript "fat arrow" notation.
Make the rewritten version as simple as possible.
Problem 7: Here are two JavaScript functions that return a function.
Rewrite each definition using only let and the "fat arrow"
notation. Make each rewritten version as simple as possible (each
one should be only one line long).
function f(u, v) {
function g(a) {
return a - u*v;
};
return g;
}
function h(u, v) {
return function(a) {
return a - u*v;
}
}
Problem 8: Evaluate each of the following JavaScript expressions.
Some of these expressions evaluate to a simple number or boolean,
but some evaluate to a function. If an expression evaluates to a
function, write the value using the JavaScript "fat arrow" notation
for anonymous functions.
(a) ( u => u*u+1 )(4)
(b) ( (u,v) => (u < v) ? (u*u) : (u-v) )(5, 4)
(c) ( function(a,b,c){return (a<b) || (a==b) && c;} )(4, 6, true)
(d) ( x => function(y){return x*x;} )(7)
(e) ( function(x){return (y)=>x*x;} )(7)(0)
(f) ( a => (u,v) => ( (r,s,t)=>r+s+t )(u,a,v) )(9) // hint: start inside
(g) ( a => (u,v) => ( (r,s,t)=>r+s+t )(u,a,v) )(9)(4, 5)
(h) ( (h,a,b) => h(a)+b )(x=>x*x, 4, 5)
(i) ( (f,g) => ( t => f(t) + g(t) ) )(x=>1+2*x, x=>x*x)
The above expressions are what JavaScript people
call IIFE's.
Problem 9: What static analysis errors would be reported
for this Language_4a program
(from homework 3)?
(prog
(var n 0)
(var a (+ b 1))
(var b 5)
(set n (if (!= a b) (< a b) (+ a b) ))
(var n 1)
(while n
(begin
(var w (+ n 1))
(set n w)
(print w))
(print w))
(print w)
)
Problem 10: Consider these two JavaScript functions.
let f = (u, v) => 3*u + 5*v;
let g = u => v => 3*u + 5*v;
(a) Explain which of these expressions is correct and why.
f(3, 4)
f(3)(4)
g(3, 4)
g(3)(4)
(b) Explain why g(5) makes sense but f(5) does not.
(c) Explain the difference between f and g.
(The function g is called the curried version of f.)