Docsity

Exam 2 Practice Questions - Programming Languages | CIS 352, Exams of Computer Science

Toggle sidebar
bg1
Exam 2 CIS 352: Programming Languages
Exam 2 CIS 352: Programming Languages
Name:
Question 1 2 3 4 5 6 7 Total
Points Received
Points Possible 12 8 20 9 12 14 25 100
Instructions — Read them all!
(i) The quiz is closed book, closed notes, and closed friends.
(ii) Legibility counts! Make sure that we can read and find your answers.
Use the backs of pages if you need more space for an answer.
(iii) Make sure that your test paper has 9 pages total.
(iv) Unless otherwise stated, you many use any built-in Scheme function you want—so long as it is
spelled and used correctly.
(v) In programming questions, define any helper functions you want.
Page 1 of 9 27 March 2008
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Exam 2 Practice Questions - Programming Languages | CIS 352 and more Exams Computer Science in PDF only on Docsity!

Exam 2 CIS 352: Programming Languages

Name:

Question 1 2 3 4 5 6 7 Total

Points Received

Points Possible 12 8 20 9 12 14 25 100

Instructions — Read them all!

(i) The quiz is closed book, closed notes, and closed friends.

(ii) Legibility counts! Make sure that we can read and find your answers. Use the backs of pages if you need more space for an answer.

(iii) Make sure that your test paper has 9 pages total.

(iv) Unless otherwise stated, you many use any built-in Scheme function you want—so long as it is spelled and used correctly.

(v) In programming questions, define any helper functions you want.

Question 1 (12 points). Suppose we have the following definitions:

(define x ’(10 20 30)) (define y ’(r a t)) (define z 100) (define lst ’( (9 8 (7)) (6 5) ((4) 3) ))

(define puzzle (let ((x 1) (y 2)) (let ((x (* 10 y)) (w x)) w) ))

Give the value of each of the following expressions.

(a) (cddar lst)

(b) (append y (cdr x))

(c) (map car lst)

(d) (apply length (list x) )

Question 2 (8 points). Suppose the following definitions are executed in the order shown.

(define a ’(Flopsy Mopsy Cottontail)) (define b (cons a (list 1))) (set! a (list 1 20 300 4000)) (define c (cdr a)) (define d ’(car a)) (set-car! (cdr d) ’Peter) (set-cdr! (cdr c) (cdr b))

What are the final values of a, b, c, and d?

Question 5 (9 points). Fill in the holes in the following tran- script. Note that the expressions are evaluated in the order shown.

(define old 100) (define prev (let ((old 0) (ans 0)) (lambda (x) (begin (set! ans old) (set! old x) ans))))

Question 4 (12 points). Beta-reduce the following expressions as far as possible, i.e., until there are no beta-redices left. Show each step and underline the redex being reduced. You may re- duce things in any order you choose.

a) ((lambda (x) (x y)) (w z))

(prev 97)

(prev 14)

b)

(lambda (x) (x x))

(lambda (z) (x y)) (z w)

old

Question 6 (14 points). Consider the following code:

%% Corrected version let b = 10 c = 20 in let f = proc(a) +(a,c) g = proc(a,c) *(b,c) in let h = proc(b) (g b (f b)) in (h 1)

Show your work for the following two questions.

a) Under lexical scoping , what is the value of this expression?

b) Under dynamic scoping , what is the value of this expression?

b. (10 points) Add if-expressions to NANOSCHEME. The concrete syntax is given by

〈expression〉 ::= (if 〈expression〉 〈expression〉 〈expression〉)

The semantics is just as in Scheme except that 0 corresponds to FALSE and any non-zero number corresponds to TRUE. You may assume the existence of functions getTest, getThen, getElse which respectively extract out the test-, then-, and else-subexpressions from an if-expression. Your answer should be of the form of a clause in the cond in eval. E.g.,

((eqv? ’if (car exp)) your code )

c. (9 points) Add while-expressions to NANOSCHEME. The concrete syntax is given by

〈expression〉 ::= (while 〈testProc〉 〈nextProc〉 〈state〉)

We assume that the subexpressions 〈testProc〉 and 〈nextProc〉 evaluate to procedures and 〈state〉 eval- uates to a number. Semantics: (while 〈testProc〉 〈nextProc〉 〈state〉) is equivalent in meaning to: ( if

〈testProc〉 〈state〉

while 〈testProc〉 〈nextProc〉 (〈nextProc〉 〈state〉)

〈state〉

You may assume the existence of functions getTestProc, getNextProc, getState which respectively extract out the testProc-, nextProc-, and state-subexpressions from a while-expression. Your answer should be of the form of a clause in eval’s cond. E.g.,

((eqv? ’while (car exp)) your code )

Feel free to create any helper functions you need. Note: A “macro-expansion” route is not the easiest way to approach this problem.