






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







This exam is closed note, closed book. Questions will be graded on quality of answer. Please supply the best answer you can to each question. Good Luck!
Fortran and Algol 60 (11 points)
(Many different answers possible here)
[A-Z] [A-Za-z0-9] [0-9]*
In the example below, it can be unclear which if stmt the else clause matches with. This can be solved in a number of ways, either by having an else should match the closest unmatched then, or by requiring explicit end markers to show the end of an IF statement.
if cond then if cond then stuff else other stuff
start
accept
A-Za-z0-
Names, Scopes, and Bindings (10 points)
int n = 100; // global
print_plus_n(int x) { cout << x + n; } increment_n() { n = n + 1; }
test() { int n; n = 1; print_plus_n(25);
n = 33; print_plus_n(n);
increment_n(); cout << n;
print_plus_n(n); }
With Static Scoping:
125 133 33 134
With Dynamic Scoping:
26 66 34 68
Functional programming (14 points)
A few reasons:
**- garbage collection (don’t need to worry about memory management)
(define y (lambda (x y) (+ x y)))
y is bound to a function that takes two parameters and returns their sum
(define b ‘(3 14 27)) (define c (cons (car (cdr b)) (list ‘a ‘b ‘c))))
What is the result of typing the following into the Scheme interpreter:
c => (14 a b c)
(car (cdr (cdr c))) => b
Example: (double_each ‘(4 5 6)) => (8 10 12)
(define (double-each x) (if (null? x) () (cons ( 2 (car x)) (double-each (cdr x)))))*
likes(mary,marlins). likes(fred,dracula). likes(fred,bats). likes(sue,yankees). likes(sue,bats). likes(fred,baseball). likes(fred,screaming). likes(fred,marlins). likes(mary,ghosts). likes(bob,cubs). likes(bob,pumpkins). likes(bob,dracula). likes(mary,bats).
friends(X,Y, Something) :- likes(X, Something), likes(Y, Something), not(X = Y).
What does this query produce? Give the complete set of responses obtained after typing ; until a “No” response is given.
?- friends(fred,X,Y).
X = bob Y = dracula ;
X = sue Y = bats ;
X = mary Y = bats ;
X = mary Y = marlins ;
No
Control Flow (10 points)
side effect – influencing subsequent computation in any way other than returning a value for use in the surrounding context.
short circuiting – (of Boolean expressions) skips over the second half of an AND if the first part is false, skips over the second part of an OR if the first part if true. (Since in both cases the value of the expression can already be determined based on the value of the first part of the expression.)
Generic example: ( false AND side effect-producing code) If short circuiting is used, then the side effect-producing code would never be executed when the first condition is false. ( cond1 AND i++) If i is meant to count the number of times the condition is tested, it will not be incremented whenever the cond1 is false.
int func (int x) { if (x == 0) return 1; else … stuff … return func (x – 1); }
An optimization is to convert the recursive call to a loop. In this way we avoid the overhead of function call and return and can re-use the stack frame from the original call (we don’t need a new stack frame for the recursive calls anymore).