Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Quiz 3b Practice Questions and Answers for CIS 352: Programming Languages, Quizzes of Computer Science

Practice questions and answers for quiz 3b in the cis 352: programming languages course. It includes definitions and expressions in a programming language with exceptions, and the addition of while-expressions to the continuation-passing interpreter.

Typology: Quizzes

Pre 2010

Uploaded on 08/09/2009

koofers-user-jph
koofers-user-jph 🇺🇸

5

(1)

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Quiz 3b Practice Questions + Answers CIS 352: Programming Languages
Question 1 (8 points).Suppose we have the following definitions in the version of our defined language that
includes exceptions.
define f = proc (n) if zero?(sub1(n)) then 50 else raise n
define h1 = proc (y) +(y,100)
define h2 = proc (z) +(z,500)
define h3 = proc (g) proc (w) +((g w), w)
Give the values for each of the following expressions:
(a) try *(10, raise 7) catch h1
Answer: 107.
try *(10, raise 7) catch h1 ;(h1 7) ;+(100,7) ;107
(b) +(12,try (f 7) catch proc (w) *(w,1000))
Answer: 7012.
Note: fapplied to an number n6=1 does a (raise n). So,
+(12,try (f 7) catch proc (w) *(w,1000)) ;+(12, (proc (w) *(w,1000) 7))
;+(12, 7000) ;7012
(c) try +(try (f 1) catch h1, 1000) catch h2
An answer. 1050.
Note: fapplied to 1 returns 50. So,
try +(try (f 1) catch h1, 1000) catch h2 ;try +(50, 1000) catch h2 ;1050
(d) try ((h3 f) 99) catch h2
An answer. 599
Note that h3 is a curried procedure. So: ((h3 f) 99) ;+((f 99),99) ;raise 99.
Thus: try ((h3 f) 99) catch h2 ;(h2 99) ;+(99,500) ;599
Question 2 (12 points).For this question we take our continuation-passing interpreter and add while-expressions
which have the following concrete and abstract syntax.
hexpi::=while hexpido hexpi
while-exp (test body)
The intended meaning of while test do body is to:
1. Evaluate test. (Call the resulting value t.)
2. If t=#f, then (the loop terminates) and the while-expression returns the value 37.
20.If t=#t, then
(i) evaluate body then
(ii) re-evaluate (while-exp test body).
Page 1 of 2 April 28, 2009
pf2

Partial preview of the text

Download Quiz 3b Practice Questions and Answers for CIS 352: Programming Languages and more Quizzes Computer Science in PDF only on Docsity!

Quiz 3b Practice Questions + Answers CIS 352: Programming Languages

Question 1 (8 points). Suppose we have the following definitions in the version of our defined language that includes exceptions.

define f = proc (n) if zero?(sub1(n)) then 50 else raise n define h1 = proc (y) +(y,100) define h2 = proc (z) +(z,500) define h3 = proc (g) proc (w) +((g w), w)

Give the values for each of the following expressions:

(a) try *(10, raise 7) catch h

Answer: 107. try *(10, raise 7) catch h1 ; (h1 7) ; +(100,7) ; 107

(b) +(12,try (f 7) catch proc (w) *(w,1000))

Answer: 7012. Note: f applied to an number n 6 = 1 does a (raise n). So, +(12,try (f 7) catch proc (w) *(w,1000)) ; +(12, (proc (w) *(w,1000) 7)) ; +(12, 7000) ; 7012

(c) try +(try (f 1) catch h1, 1000) catch h An answer. 1050. Note: f applied to 1 returns 50. So, try +(try (f 1) catch h1, 1000) catch h2 ; try +(50, 1000) catch h2 ; 1050

(d) try ((h3 f) 99) catch h

An answer. 599 Note that h3 is a curried procedure. So: ((h3 f) 99) ; +((f 99),99) ; raise 99. Thus: try ((h3 f) 99) catch h2 ; (h2 99) ; +(99,500) ; 599

Question 2 (12 points). For this question we take our continuation-passing interpreter and add while-expressions which have the following concrete and abstract syntax.

〈exp〉 ::= while 〈exp〉 do 〈exp〉 while-exp (test body)

The intended meaning of “while test do body” is to:

1. Evaluate test. (Call the resulting value t.) 2. If t =#f, then (the loop terminates) and the while-expression returns the value 37.

2. If t =#t, then

(i) evaluate body then (ii) re-evaluate (while-exp test body).

Page 1 of 2 April 28, 2009

Quiz 3b Practice Questions + Answers CIS 352: Programming Languages

For example, the following should print out: 2 4 8 16:

let x = 1 k = 4 in while notZero?(k) do begin set x = *(x,2); set k = -(k,1); print(x) end A relevant portion of value-of and apply-cont from the continuation-passing interpreter is given below. Write the necessary code to implement the while construct.

(define value-of/k (lambda (exp env cont) (cases expression exp (const-exp (num) (apply-cont cont (num-val num))) (var-exp (var) (apply-cont cont (apply-env env var))) ... (if-exp (exp1 exp2 exp3) (value-of/k exp1 env (if-test-cont exp2 exp3 env cont))) (while-exp (test body) (value-of/k test env (while-test-cont test body env cont))) ... )))

(define apply-cont (lambda (cont val) (cases continuation cont (end-cont () val) (if-test-cont (exp2 exp3 env cont) (if (expval->bool val) (value-of/k exp2 env cont) (value-of/k exp3 env cont))) ... (while-test-cont (test body saved-env saved-cont) (if (not (expval->bool val)) (apply-cont saved-cont (num-val 37))

... Your code 2(i) will go here... ))

(while-body-cont (test body saved-env saved-cont)

... Your code 2(ii) will go here... ) )))

An answer for the 2(i) slot:

(value-of/k body saved-env (while-body-cont test body saved-env saved-cont))

An answer for the 2(ii) slot:

(value-of/k (while-exp test body) saved-env saved-cont)

Page 2 of 2 April 28, 2009