dprogsprog nal exam, june 2010 -...

39
dProgSprog final exam, June 2010 ID Name Remember to show us an ID when returning the present exam form. We will then mark your name on our master list. This document contains 62 questions and is structured in 11 groups of questions. Strategically, you should first scan it quickly in its entirety, and identify the groups of questions that seem easiest to you. You should address them first and then move on to the others. All the Scheme definitions are in the standard initial environment of Scheme. All the Scheme and Schelog programs are syntactically correct (in particular, they are well parenthesized). For each question, there is one single correct answer. For better or for worse, there are no questions about types: the questions are pretty much about everything else. Happy working!

Upload: others

Post on 15-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

dProgSprog final exam, June 2010

ID

Name

Remember to show us an ID when returning the present exam form.We will then mark your name on our master list.

This document contains 62 questions and is structured in 11 groups of questions. Strategically, youshould first scan it quickly in its entirety, and identify the groups of questions that seem easiest toyou. You should address them first and then move on to the others.

• All the Scheme definitions are in the standard initial environment of Scheme.

• All the Scheme and Schelog programs are syntactically correct(in particular, they are well parenthesized).

• For each question, there is one single correct answer.

• For better or for worse, there are no questions about types: the questions are pretty much abouteverything else.

Happy working!

Page 2: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a
Page 3: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

– 1 question about interpreters –

Question 1In your mind, running n interpreters on top of each other incurs a time complexity that is:

a constantb linear in n

2 c quadratic in nd cubic in n

1 e polynomial in n with a degree strictly greater than 3

69 f x exponential in n

Page 4: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

– 8 questions about list processing –(and to hammer out why pretty printing is good for you)

Here is the definition of a Scheme procedure:

(define map2 (lambda (f xs ys) (letrec ([visit (lambda (xs ys) (if

(and (pair? xs) (pair? ys)) (cons (f (car xs) (car ys)) (visit (cdr

xs) (cdr ys))) ’()))]) (visit xs ys))))

Hint, hint: start by writing the definition above with line breaks, indentation, etc., as we teacherand teaching assistants have had to do with your own definitions so many times in the course of thequarter.

Question 2What is the result of evaluating (map2 + ’(1 2) ’(2 1))?

1 a an errorb ()

67 c x (3 3)

4 d ((+ ’1 ’2) (+ ’2 ’1))

e no result, because the computation diverges

Question 3What is the result of evaluating (map2 + (1 2) (2 1))?

47 a x an error

3 b ()

19 c (3 3)

3 d ((+ 1 2) (+ 2 1))

e no result, because the computation diverges

Question 4What is the result of evaluating (map2 ’+ ’(1 2) ’(2 1))?

41 a x an errorb ()

8 c (3 3)

19 d ((’+ ’1 ’2) (’+ ’2 ’1))

4 e (’(+ 2 1) ’(+ 1 2))

f no result, because the computation diverges

Page 5: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 5What is the result of evaluating (map2 + ’(1 2) ’(2 1 0))?

3 a an errorb ()

65 c x (3 3)

3 d (3 3 0)

1 e ((+ ’1 ’2) (+ ’2 ’1))

4 f ((+ ’1 ’2) (+ ’2 ’1) (+ ’() ’0))

1 g no result, because the computation diverges

Question 6What is the result of evaluating (map2 + ’(1 2 3) ’(2 1))?

4 a an errorb ()

61 c x (3 3)

6 d (3 3 3)

4 e ((+ ’1 ’2) (+ ’2 ’1) (+ ’3 ’()))

1 f ((+ ’1 ’2) (+ ’2 ’1) (+ ’3))

1 g no result, because the computation diverges

Question 7What is the result of evaluating (map2 + ’() ’())?

3 a an error

66 b x ()

2 c (())

d ((+))

4 e ((+ ’() ’()))

1 f no result, because the computation diverges

Question 8What is the result of evaluating (map2 + 10 100)?

6 a an error

57 b x ()

2 c 110

5 d (110)

2 e (+ 10 100)

1 f ((+ 10 100))

1 g no result, because the computation diverges

Page 6: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 9What is the result of evaluating (map2 + ’(1 2 . 3) ’(2 1 0))?

12 a an error

1 b ()

43 c x (3 3)

11 d (3 3 3)

7 e (3 3 . 3)

2 f ((+ ’1 ’2) (+ ’2 ’1))

1 g ((+ ’1 ’2) (+ ’2 ’1) (+ ’3 ’0))

1 h ((+ ’1 ’2) (+ ’2 ’1) (+ . ’3 ’0))

1 i no result, because the computation diverges

Page 7: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

– 5 questions aboutparallel (let), sequential (let*) and recursive (letrec) declarations –

Question 10What is the result of evaluating the following expression?

(let ([x 1]

[y 10]

[z 100])

(let ([y 20])

(let ([x (+ x y)]

[y (+ x z)])

(list x y z))))

1 a an error

65 b x (21 101 100)

1 c (1 10 100)

d (11 41 100)

4 e (21 121 100)

f no result, because the computation diverges

Question 11What is the result of evaluating the following expression?

(let* ([x 1]

[y 10]

[z 100])

(let* ([y 20])

(let* ([x (+ x y)]

[y (+ x z)])

(list x y z))))

1 a an error

6 b (21 101 100)

c (1 10 100)

d (11 41 100)

64 e x (21 121 100)

1 f no result, because the computation diverges

Page 8: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 12What is the result of evaluating the following expression?

(let ([x 1]

[y 10]

[z 100])

(let ([y (lambda (x)

(+ x y))])

(let ([x (y x)]

[y (+ x z)])

(list x y z))))

7 a an errorb (21 <procedure> 100)

1 c (11 <procedure> 100)

12 d (11 111 100)

54 e x (11 101 100)

f no result, because the computation diverges

Question 13What is the result of evaluating the following expression?

(let* ([x 1]

[y 10]

[z 100])

(let* ([y (lambda (x)

(+ x y))])

(let* ([x (y x)]

[y (+ x z)])

(list x y z))))

10 a an errorb (21 <procedure> 100)

3 c (11 <procedure> 100)

55 d x (11 111 100)

4 e (11 101 100)

1 f no result, because the computation diverges

Page 9: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 14What is the result of evaluating the following expression?

(letrec ([fac (lambda (n)

(if (= n 0)

1

(* n (fac (- n 1)))))])

(letrec ([f (lambda (x f)

(f x))])

(f 5 fac)))

6 a an error

64 b x 120

1 c no result, because the computation diverges

Page 10: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

– 18 questions about parameter passing in Scheme –

The following 3 Scheme procedures use the predefined procedure length that, given a proper list,returns the length of this list.

(define rip

(lambda (x)

(length x)))

(define rap

(lambda y

(length y)))

(define rup

(lambda (a b . c)

(length c)))

Question 15What is the result of evaluating (rip)?

68 a x a run-time errorb -1

6 c 0d 1e 2f 3g 4h 5

2 i no result, because the computation diverges

Question 16What is the result of evaluating (rip 0)?

63 a x a run-time errorb -1

1 c 0

9 d 1e 2f 3g 4h 5

2 i no result, because the computation diverges

Page 11: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 17What is the result of evaluating (rip 0 1 2 3)?

65 a x a run-time errorb -1

1 c 0

1 d 1e 2f 3

6 g 4h 5

1 i no result, because the computation diverges

Question 18What is the result of evaluating (rip ’())?

2 a a run-time errorb -1

70 c x 0

1 d 1e 2f 3g 4h 5i no result, because the computation diverges

Question 19What is the result of evaluating (rip ’(0))?

a a run-time errorb -1

4 c 0

70 d x 1e 2f 3g 4h 5i no result, because the computation diverges

Page 12: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 20What is the result of evaluating (rip ’(0 1 2 3))?

a a run-time errorb -1c 0d 1e 2f 3

71 g x 4

1 h 5i no result, because the computation diverges

Question 21What is the result of evaluating (rap)?

45 a a run-time errorb -1

28 c x 0d 1e 2f 3g 4h 5

1 i divergence

Question 22What is the result of evaluating (rap 0)?

41 a a run-time errorb -1

2 c 0

32 d x 1e 2f 3g 4h 5

1 i no result, because the computation diverges

Page 13: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 23What is the result of evaluating (rap 0 1 2 3)?

42 a a run-time errorb -1c 0

1 d 1e 2f 3

28 g x 4h 5i no result, because the computation diverges

Question 24What is the result of evaluating (rap ’())?

29 a a run-time errorb -1

24 c 0

21 d x 1e 2f 3g 4h 5

2 i no result, because the computation diverges

Question 25What is the result of evaluating (rap ’(0))?

30 a a run-time errorb -1

1 c 0

42 d x 1

1 e 2f 3g 4h 5i no result, because the computation diverges

Page 14: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 26What is the result of evaluating (rap ’(0 1 2 3))?

29 a a run-time errorb -1c 0

24 d x 1e 2f 3

25 g 4

1 h 5i no result, because the computation diverges

Question 27What is the result of evaluating (rup)?

69 a x a run-time errorb -1

4 c 0d 1e 2f 3g 4h 5

3 i no result, because the computation diverges

Question 28What is the result of evaluating (rup 0)?

65 a x a run-time errorb -1

6 c 0

4 d 1e 2f 3g 4h 5

4 i no result, because the computation diverges

Page 15: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 29What is the result of evaluating (rup 0 1 2 3)?

42 a a run-time errorb -1c 0

3 d 1

24 e x 2

1 f 3

3 g 4h 5

1 i no result, because the computation diverges

Question 30What is the result of evaluating (rup ’())?

60 a x a run-time errorb -1

13 c 0d 1e 2f 3g 4h 5

1 i no result, because the computation diverges

Question 31What is the result of evaluating (rup ’(0))?

58 a x a run-time errorb -1

6 c 0

7 d 1e 2f 3g 4h 5

1 i no result, because the computation diverges

Page 16: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 32What is the result of evaluating (rup ’(0 1 2 3))?

56 a x a run-time errorb -1

1 c 0

2 d 1

4 e 2

2 f 3

6 g 4h 5

1 i no result, because the computation diverges

Page 17: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

– 6 questions about evaluation order –

You are given the following definitions:

(define foo

(lambda (x y z)

(+ x (+ x y))))

(define traced-factorial

(trace-lambda "factorial" (n)

(if (= n 0)

1

(* n (traced-factorial (- n 1))))))

The goal of this series of questions is to evaluate

(foo (traced-factorial 0) (traced-factorial 2) (traced-factorial -3))

under a variety of evaluation orders: call by name, call by value, etc.

Question 33Assuming a version of Scheme with left-to-right call by value (i.e., where sub-expressions in anapplication are evaluated from left to right), what are the effect and the result of the evaluation?

4 a a run-time error

3 b no trace and no result, because the computation diverges

52 c x the trace of factorial 0, and then of factorial 2, and then of factorial-3, which diverges and therefore yields no result

1 d the trace of factorial -3, which diverges and therefore yields noresult

8 e the trace of factorial 0, and then of factorial 2, and then of factorial-3, with 4 as result

1 f the trace of factorial -3, and then of factorial 2, and then of factorial0, with 4 as result

3 g the trace of factorial 0, and then of factorial 0, and then of factorial2, with 4 as result

1 h the trace of factorial 2, and then of factorial 0, and then of factorial0, with 4 as result

i the trace of factorial 2, and then of factorial 0, with 4 as result

1 j the trace of factorial 0, and then of factorial 2, with 4 as result

Page 18: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 34Assuming a version of Scheme with right-to-left call by value (i.e., where sub-expressions in anapplication are evaluated from right to left), what are the effect and the result of the evaluation?

6 a a run-time error

11 b no trace and no result, because the computation diverges

12 c the trace of factorial 0, and then of factorial 2, and then of factorial-3, which diverges and therefore yields no result

39 d x the trace of factorial -3, which diverges and therefore yields noresult

5 e the trace of factorial 0, and then of factorial 2, and then of factorial-3, with 4 as result

5 f the trace of factorial -3, and then of factorial 2, and then of factorial0, with 4 as result

g the trace of factorial 0, and then of factorial 0, and then of factorial2, with 4 as result

2 h the trace of factorial 2, and then of factorial 0, and then of factorial0, with 4 as result

i the trace of factorial 2, and then of factorial 0, with 4 as resultj the trace of factorial 0, and then of factorial 2, with 4 as result

Question 35Assuming a version of Scheme with call by name and where the arguments of a primitive proceduresuch as + are evaluated from left to right, what are the effect and the result of the evaluation?

1 a a run-time error

2 b no trace and no result, because the computation diverges

3 c the trace of factorial 0, and then of factorial 2, and then of factorial-3, which diverges and therefore yields no result

1 d the trace of factorial -3, which diverges and therefore yields noresult

e the trace of factorial 0, and then of factorial 2, and then of factorial-3, with 4 as result

f the trace of factorial -3, and then of factorial 2, and then of factorial0, with 4 as result

61 g x the trace of factorial 0, and then of factorial 0, and then of factorial2, with 4 as result

1 h the trace of factorial 2, and then of factorial 0, and then of factorial0, with 4 as result

1 i the trace of factorial 2, and then of factorial 0, with 4 as result

5 j the trace of factorial 0, and then of factorial 2, with 4 as result

Page 19: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 36Assuming a version of Scheme with call by name and where the arguments of a primitive proceduresuch as + are evaluated from right to left, what are the effect and the result of the evaluation?

2 a a run-time error

3 b no trace and no result, because the computation diverges

1 c the trace of factorial 0, and then of factorial 2, and then of factorial-3, which diverges and therefore yields no result

2 d the trace of factorial -3, which diverges and therefore yields noresult

1 e the trace of factorial 0, and then of factorial 2, and then of factorial-3, with 4 as result

f the trace of factorial -3, and then of factorial 2, and then of factorial0, with 4 as result

1 g the trace of factorial 0, and then of factorial 0, and then of factorial2, with 4 as result

59 h x the trace of factorial 2, and then of factorial 0, and then of factorial0, with 4 as result

6 i the trace of factorial 2, and then of factorial 0, with 4 as result

1 j the trace of factorial 0, and then of factorial 2, with 4 as result

Question 37Assuming a version of Scheme with call by need and where the arguments of a primitive proceduresuch as + are evaluated from left to right, what are the effect and the result of the evaluation?

1 a a run-time error

2 b no trace and no result, because the computation diverges

1 c the trace of factorial 0, and then of factorial 2, and then of factorial-3, which diverges and therefore yields no result

1 d the trace of factorial -3, which diverges and therefore yields noresult

2 e the trace of factorial 0, and then of factorial 2, and then of factorial-3, with 4 as result

1 f the trace of factorial -3, and then of factorial 2, and then of factorial0, with 4 as result

4 g the trace of factorial 0, and then of factorial 0, and then of factorial2, with 4 as result

h the trace of factorial 2, and then of factorial 0, and then of factorial0, with 4 as result

4 i the trace of factorial 2, and then of factorial 0, with 4 as result

60 j x the trace of factorial 0, and then of factorial 2, with 4 as result

Page 20: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 38Assuming a version of Scheme with call by need and where the arguments of a primitive proceduresuch as + are evaluated from right to left, what are the effect and the result of the evaluation?

3 a a run-time error

4 b no trace and no result, because the computation diverges

1 c the trace of factorial 0, and then of factorial 2, and then of factorial-3, which diverges and therefore yields no result

1 d the trace of factorial -3, which diverges and therefore yields noresult

1 e the trace of factorial 0, and then of factorial 2, and then of factorial-3, with 4 as result

f the trace of factorial -3, and then of factorial 2, and then of factorial0, with 4 as result

g the trace of factorial 0, and then of factorial 0, and then of factorial2, with 4 as result

4 h the trace of factorial 2, and then of factorial 0, and then of factorial0, with 4 as result

59 i x the trace of factorial 2, and then of factorial 0, with 4 as result

4 j the trace of factorial 0, and then of factorial 2, with 4 as result

Page 21: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

– 2 questions about set processing –

The following Scheme procedure tests whether an element occurs in a proper list:

(define member?

(lambda (x xs)

(letrec ([visit (lambda (xs)

(cond

[(null? xs)

#f]

[(equal? x (car xs))

#t]

[else

(visit (cdr xs))]))])

(visit xs))))

Counting beans,

• there is 1 call to visit when evaluating (member? 10 ’())

• there are 2 calls to visit when evaluating (member? 10 ’(20))

• there are 2 calls to visit when evaluating (member? 20 ’(10 20 30))

• there are 3 calls to visit when evaluating (member? 30 ’(10 20 30))

• there are 4 calls to visit when evaluating (member? 40 ’(10 20 30))

In the rest of this question, sets are represented as a proper list of their elements, without repetition.So:

• () represents the empty set

• (1 2 3) represents the set containing the elements 1, 2 and 3

• (10 10) does not represent a set

Page 22: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 39Here is a definition of set union:

(define set-union-foo

(lambda (s1 s2)

(letrec ([visit-foo (lambda (xs ys)

(cond

[(null? xs)

ys]

[(member? (car xs) s2)

(visit-foo (cdr xs) ys)]

[else

(visit-foo (cdr xs) (cons (car xs) ys))]))])

(visit-foo s1 s2))))

How many calls to visit (as locally defined in the definition of member? above) are there whenevaluating the following expression?

(set-union-foo ’(10 20 30 40) ’(100 200 300 400 500 600 700 800))

a 0

1 b 1c 31

5 d 32

2 e 33

54 f x 36

3 g 40

5 h 42

1 i 45j an infinity, since running this code diverges

4 k it does not matter because an error occurs

Page 23: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 40Here is a definition of set union:

(define set-union-bar

(lambda (s1 s2)

(letrec ([visit-bar (lambda (xs ys)

(cond

[(null? xs)

ys]

[(member? (car xs) ys)

(visit-bar (cdr xs) ys)]

[else

(visit-bar (cdr xs) (cons (car xs) ys))]))])

(visit-bar s1 s2))))

How many calls to visit (as locally defined in the definition of member? above) are there whenevaluating the following expression?

(set-union-bar ’(10 20 30 40) ’(100 200 300 400 500 600 700 800))

a 0

1 b 1c 31

3 d 32

1 e 33

6 f 36

4 g 40

55 h x 42

1 i 45

1 j an infinity, since running this code diverges

1 k it does not matter because an error occurs

Page 24: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

– 3 questions about higher-order programming –

Reminder: the BNF of Boolean expressions reads as

e ::= (var x) | (neg e) | (conj e e) | (disj e e)

and thus we implement it with

• the constructors make-var, make-neg, etc.

• the predicates var?, neg?, etc.

• the accessors var 1, neg 1, etc.

Similarly, the BNF of Boolean expressions in negational normal form reads as

e_nnf ::= (posvar x)

| (negvar x)

| (conj_nnf e_nnf e_nnf)

| (disj_nnf e_nnf e_nnf)

and thus we implement it with make-posvar, etc.The fold procedure associated to Boolean expressions reads as

(define boolean-expression-fold-right

(lambda (process-var process-neg process-conj process-disj)

(lambda (e)

(letrec ([visit

(lambda (e)

(cond

[(var? e)

(process-var (var_1 e))]

[(neg? e)

(process-neg (visit (neg_1 e)))]

[(conj? e)

(process-conj (visit (conj_1 e)) (visit (conj_2 e)))]

[(disj? e)

(process-disj (visit (disj_1 e)) (visit (disj_2 e)))]

[else

(errorf ’boolean-expression-fold-right

"not a Boolean expression: ~s"

e)]))])

(visit e)))))

Page 25: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

The goal of the following 3 questions is to study the following procedure:

(define boolean-magic

(lambda (e)

(car ((boolean-expression-fold-right

(lambda (x)

(cons (make-posvar x) (make-negvar x)))

(lambda (p)

(cons (cdr p) (car p)))

(lambda (p1 p2)

(cons (make-conj_nnf (car p1) (car p2))

(make-disj_nnf (cdr p1) (cdr p2))))

(lambda (p1 p2)

(cons (make-disj_nnf (car p1) (car p2))

(make-conj_nnf (cdr p1) (cdr p2)))))

e))))

Question 41What is the result of evaluating:

(boolean-magic ’(neg (neg (var x))))

1 a (negvar x)

38 b x (posvar x)

22 c ((posvar x) negvar x)

d x

3 e (var x)

f (neg (neg (var x)))

3 g an error

2 h no result, because the computation diverges

Question 42What is the result of evaluating:

(boolean-magic ’(conj (neg (var x)) (var y)))

4 a (disj nnf (posvar x) (negvar y))

37 b x (conj nnf (negvar x) (posvar y))

26 c ((conj nnf (negvar x) (posvar y)) disj nnf (posvar x) (negvar y))

2 d (conj (neg (var x)) (var y))

1 e an errorf no result, because the computation diverges

Page 26: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 43What is the result of evaluating

(boolean-magic ’(conj (neg (disj (var x) (neg (var z)))) (var y)))

1 a (disj nnf (disj nnf (posvar x) (negvar z)) (negvar y))

32 b x (conj nnf (conj nnf (negvar x) (posvar z)) (posvar y))

26 c ((conj nnf (conj nnf (negvar x) (posvar z)) (posvar y))

disj nnf (disj nnf (posvar x) (negvar z)) (negvar y))

1 d (conj (neg (disj (var x) (neg (var z)))) (var y))

2 e (disj nnf (negvar y) (disj nnf (negvar z) (posvar x)))

1 f (conj nnf (posvar y) (conj nnf (posvar z) (negvar x)))

1 g an errorh no result, because the computation diverges

Page 27: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

– 5 questions about stream processing –

The following procedure constructs a stream:

(define make-stream

(lambda (seed next)

(letrec ([visit (lambda (current)

(cons current (lambda ()

(visit (next current)))))])

(visit seed))))

For example, the stream of natural numbers is defined as follows:

(define nats

(make-stream 0 (lambda (n)

(+ n 1))))

The following procedure maps the prefix of a stream into a list:

(define stream->list

(lambda (xs n)

(if (or (null? xs) (= n 0))

’()

(cons (car xs)

(stream->list ((cdr xs)) (- n 1))))))

For example, evaluating (stream->list nats 10) yields the list of the 10 first natural numbers,i.e., (0 1 2 3 4 5 6 7 8 9).

The goal of the following 5 questions is to complete the following definition by replacing AAAAAA,..., EEEEEE by appropriate Scheme expressions:

(define rake-stream

(lambda (xs b)

(letrec ([one (lambda (ys)

(if (null? ys)

’()

(two AAAAAA)))]

[two (lambda (zs)

(if (null? zs)

’()

(cons BBBBBB CCCCCC)))])

(if b

DDDDDD

EEEEEE))))

Page 28: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

This procedure maps an input stream into an output stream that contains either the 1st, 3rd, 5th,etc., or the 2nd, 4th, 6th, etc. elements of the input stream, so that evaluating

(stream->list (rake-stream nats #t) 10)

yields the 10 first even natural numbers, i.e., (0 2 4 6 8 10 12 14 16 18), and that evaluating

(stream->list (rake-stream nats #f) 6)

yields the first 6 odd natural numbers, i.e., (1 3 5 7 9 11).

Question 44What should AAAAAA be?

a ys

b (ys)

c (car ys)

1 d (car (ys))

39 e (cdr ys)

4 f (cdr (ys))

21 g x ((cdr ys))

2 h ((cdr (ys)))

1 i (((cdr ys)))

1 j (((cdr (ys))))

k (cddr ys)

l (cddr (ys))

m ((cddr ys))

n ((cddr (ys)))

o (one ys)

p (one (ys))

2 q (one (cdr ys))

1 r (one ((cdr ys)))

s (one (((cdr ys))))

t (one (cdr (ys)))

u (one ((cdr (ys))))

v (one (((cdr (ys)))))

Question 45What should BBBBBB be?

a zs

63 b x (car zs)

4 c ((car zs))

4 d (car (zs))

2 e ((car (zs)))

Page 29: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 46What should CCCCCC be?

a zs

1 b (cdr zs)

c (cdr (zs))

1 d ((cdr (zs)))

19 e x (lambda () (one ((cdr zs))))

34 f (lambda () (one (cdr zs)))

9 g (lambda () (one (lambda () (cdr zs))))

3 h (lambda () (one (lambda () (cdr (zs)))))

4 i (lambda () (one (lambda () ((cdr (zs))))))

2 j (lambda () (two ((cdr zs))))

4 k (lambda () (two (cdr zs)))

l (lambda () (two (lambda () (cdr zs))))

m (lambda () (two (lambda () (cdr (zs)))))

1 n (lambda () (two (lambda () ((cdr (zs))))))

Question 47What should DDDDDD be?

13 a (one xs)

55 b x (two xs)

Question 48What should EEEEEE be?

56 a x (one xs)

12 b (two xs)

Page 30: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

– 3 questions about object-oriented programming –

Given 2 initial coordinates in a plane, the following procedure creates a 2-dimensional point:

(define make-2-dimensional-point

(lambda (x-init y-init)

(let ([x x-init]

[y y-init])

(lambda (msg)

(case msg

[(location)

(list x y)]

[(x)

(list x)]

[(x+)

(begin

(set! x (+ x 1))

(list))]

[(x-)

(begin

(set! x (- x 1))

(list))]

[(y)

(list y)]

[(y+)

(begin

(set! y (+ y 1))

(list))]

[(y-)

(begin

(set! y (- y 1))

(list))]

[(reset)

(begin

(set! x x-init)

(set! y y-init)

(list))]

[(init?)

(list (and (= x x-init) (= y y-init)))]

[else

(list "Hvad siger du?")])))))

A point navigates in the plane when it is sent either of the messages x+, x-, y+ and y-. Sendingit the message location makes it yields its current coordinates in the plane. The coordinates of apoint can be re-initialized by sending it the message reset, and one can also check whether a pointis at its initial position by sending it the message init?. The result of sending a message to a pointis a list, possibly empty.

Page 31: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Given an initial third coordinate in space and a 2-dimensional point, the following procedure createsa 3-dimensional point:

(define add-a-third-dimension

(lambda (2-dimensional-point z-init)

(let ([z z-init])

(lambda (msg)

(case msg

[(location)

(let ([2-location (2-dimensional-point ’location)])

(list (list-ref 2-location 0) (list-ref 2-location 1) z))]

[(z)

(list z)]

[(z+)

(begin

(set! z (+ z 1))

(list))]

[(z-)

(begin

(set! z (- z 1))

(list))]

[(reset)

(begin

(set! z z-init)

(2-dimensional-point ’reset))]

[(init?)

(if (= z z-init)

(2-dimensional-point ’init?)

(list #f))]

[else

(2-dimensional-point msg)])))))

A 3-dimensional point inherits the coordinates of its initial 2-dimensional point. It navigates in spacewhen it is sent either of the messages x+, x-, y+, y-, z+ and z-. It processes the messages z+ and z-,and delegates the messages x+, x-, y+ and y- to its initial 2-dimensional point. It otherwise behavesas a 2-dimensional point, and reacts to the messages location, reset, and init?.

Page 32: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 49After the following interaction

> (define xy (make-2-dimensional-point 0 0))

> (xy ’location)

(0 0)

> (xy ’x+)

()

> (xy ’y+)

()

> (xy ’reset)

()

> (xy ’x+)

()

>

what is the result of evaluating (xy ’location)?

a (2 1)

b (1 2)

c (0 1)

69 d x (1 0)

e ("Hvad siger du?")

1 f an error

Question 50After the following interaction

> (define xy (make-2-dimensional-point 10 100))

> (define xyz (add-a-third-dimension xy 1000))

> (xyz ’x+)

()

> (xyz ’y+)

()

> (xyz ’z+)

()

> (xy ’reset)

()

>

what is the result of evaluating (xyz ’location)?

12 a (11 101 1001)

61 b x (10 100 1001)

c ("Hvad siger du?")

1 d an error

Page 33: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 51After the following interaction

> (define for_each

(lambda (point msgs)

(letrec ([visit (lambda (msgs)

(if (null? msgs)

’()

(begin

(point (car msgs))

(visit (cdr msgs)))))])

(visit msgs))))

> (define xy (make-2-dimensional-point 0 0))

> (for_each xy ’(x- y+ reset init? x+ y- "hello world"))

()

>

what is the result of evaluating (xy ’location)?

6 a (0 0)

b (-1 1)

c (1 1)

d (-1 -1)

50 e x (1 -1)

15 f ("Hvad siger du?")

1 g an error

Page 34: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

– 8 questions about writing a logic program –

Assume the following syntax constructors in Schelog:

(define Var

(lambda (symbol)

(vector ’Var symbol)))

(define Not

(lambda (formula)

(vector ’Not formula)))

(define And

(lambda (formula1 formula2)

(vector ’And formula1 formula2)))

(define Or

(lambda (formula1 formula2)

(vector ’Or formula1 formula2)))

The goal of the 8 following questions is to complete the following negational normalizer:

(define %normalize

(%rel (F F_nf)

[(F F_nf)

(%normalize_pos F F_nf)]))

(define %normalize_pos

(%rel (X F F1 F2 F_nf F1_nf F2_nf)

[((Var X) (Var X))

...]

[((Not F) F_nf)

...]

[((And F1 F2) (And F1_nf F2_nf))

...]

[((Or F1 F2) (Or F1_nf F2_nf))

...]))

(define %normalize_neg

(%rel (X F F1 F2 F_nf F1_nf F2_nf)

[((Var X) (Not (Var X)))

...]

[((Not F) F_nf)

...]

[((And F1 F2) (Or F1_nf F2_nf))

...]

[((Or F1 F2) (And F1_nf F2_nf))

...]))

Page 35: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 52What would you write in the Var clause of %normalize pos?

50 a x nothing

2 b (%normalize neg X X)

8 c (%normalize pos X X)

6 d (%normalize X X)

Question 53What would you write in the Not clause of %normalize pos?

1 a nothing

62 b x (%normalize neg F F nf)

3 c (%normalize pos F F nf)

Question 54What would you write in the And clause of %normalize pos?

a nothing

4 b (%normalize neg F1 F1 nf) (%normalize neg F2 F2 nf)

3 c (%normalize pos F1 F1 nf) (%normalize neg F2 F2 nf)

4 d (%normalize neg F1 F1 nf) (%normalize pos F2 F2 nf)

55 e x (%normalize pos F1 F1 nf) (%normalize pos F2 F2 nf)

Question 55What would you write in the Or clause of %normalize pos?

a nothing

6 b (%normalize neg F1 F1 nf) (%normalize neg F2 F2 nf)

6 c (%normalize pos F1 F1 nf) (%normalize neg F2 F2 nf)

5 d (%normalize neg F1 F1 nf) (%normalize pos F2 F2 nf)

48 e x (%normalize pos F1 F1 nf) (%normalize pos F2 F2 nf)

Question 56What would you write in the Var clause of %normalize neg?

44 a x nothing

8 b (%normalize neg X X)

4 c (%normalize pos X X)

8 d (%normalize X X)

Question 57What would you write in the Not clause of %normalize neg?

1 a nothing

9 b (%normalize neg F F nf)

57 c x (%normalize pos F F nf)

Page 36: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 58What would you write in the And clause of %normalize neg?

1 a nothing

49 b x (%normalize neg F1 F1 nf) (%normalize neg F2 F2 nf)

3 c (%normalize pos F1 F1 nf) (%normalize neg F2 F2 nf)

3 d (%normalize neg F1 F1 nf) (%normalize pos F2 F2 nf)

9 e (%normalize pos F1 F1 nf) (%normalize pos F2 F2 nf)

Question 59What would you write in the Or clause of %normalize neg?

2 a nothing

41 b x (%normalize neg F1 F1 nf) (%normalize neg F2 F2 nf)

4 c (%normalize pos F1 F1 nf) (%normalize neg F2 F2 nf)

6 d (%normalize neg F1 F1 nf) (%normalize pos F2 F2 nf)

12 e (%normalize pos F1 F1 nf) (%normalize pos F2 F2 nf)

Page 37: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

– 3 questions about logic programming –

Assume the following syntax constructors in Schelog:

(define var

(lambda (x)

(vector ’var x)))

(define neg

(lambda (e)

(vector ’neg e)))

(define conj

(lambda (e1 e2)

(vector ’conj e1 e2)))

(define disj

(lambda (e1 e2)

(vector ’disj e1 e2)))

(define posvar

(lambda (x)

(vector ’posvar x)))

(define negvar

(lambda (x)

(vector ’negvar x)))

(define conj_nnf

(lambda (e1 e2)

(vector ’conj_nnf e1 e2)))

(define disj_nnf

(lambda (e1 e2)

(vector ’disj_nnf e1 e2)))

These syntax constructors are meant for the BNF of Boolean expressions

e ::= (var x) | (neg e) | (conj e e) | (disj e e)

and for the BNF of Boolean expressions in negational normal form:

e_nnf ::= (posvar x)

| (negvar x)

| (conj_nnf e_nnf e_nnf)

| (disj_nnf e_nnf e_nnf)

NB. Recall that a vector prints as a list of its elements, prefixed with “#”,so that evaluating (conj (var ’x) (var ’y)) yields “#(conj #(var x) #(var y))”.

Page 38: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

The goal of the following 3 questions is to study the following relations:

(define %relate-boolean-expression_aux

(%rel (X E E- E+ E1 E1+ E1- E2 E2+ E2-)

[((var X) (posvar X) (negvar X))]

[((neg E) E+ E-)

(%relate-boolean-expression_aux E E- E+)]

[((conj E1 E2) (conj_nnf E1+ E2+) (disj_nnf E1- E2-))

(%relate-boolean-expression_aux E1 E1+ E1-)

(%relate-boolean-expression_aux E2 E2+ E2-)]

[((disj E1 E2) (disj_nnf E1+ E2+) (conj_nnf E1- E2-))

(%relate-boolean-expression_aux E1 E1+ E1-)

(%relate-boolean-expression_aux E2 E2+ E2-)]))

(define %relate-boolean-expression

(%rel (E E+ E-)

[(E E+)

(%relate-boolean-expression_aux E E+ E-)]))

Question 60What is the result of evaluating:

(%which (X)

(%relate-boolean-expression

(neg (neg (var ’x)))

X))

2 a ((X #(negvar x)))

48 b x ((X #(posvar x)))

c ((X x))

4 d ((X #(var x)))

6 e ((X #(neg #(neg #(var x)))

1 f an errorg no result, because the computation divergesh #f

Page 39: dProgSprog nal exam, June 2010 - users-cs.au.dkusers-cs.au.dk/danvy/dProgSprog16/Supplementary... · Question 1 In your mind, running n interpreters on top of each other incurs a

Question 61What is the result of evaluating:

(%which (X)

(%relate-boolean-expression

(conj (neg (var ’x)) (var ’y))

X))

3 a ((X #(disj nnf #(posvar x) #(negvar y))))

48 b x ((X #(conj nnf #(negvar x) #(posvar y))))

10 c ((X #(conj #(neg #(var x)) #(var y))))

1 d an errore no result, because the computation divergesf #f

Question 62What is the result of evaluating:

(%which (X)

(%relate-boolean-expression

(conj (neg (disj (var ’x) (neg (var ’z)))) (var ’y))

X))

2 a ((X #(disj nnf #(disj nnf #(posvar x) #(negvar z)) #(negvar y))))

49 b x ((X #(conj nnf #(conj nnf #(negvar x) #(posvar z)) #(posvar y))))

5 c ((X #(conj #(neg #(disj #(var x) #(neg #(var z)))) #(var y))))

d ((X #(disj nnf #(negvar y) #(disj nnf #(negvar z) #(posvar x)))))

1 e ((X #(conj nnf #(posvar y) #(conj nnf #(posvar z) #(negvar x)))))

1 f an errorg no result, because the computation divergesh #f