This exam is worth 100 points. You have two hours to answer six questions. Good luck!
null?, pair?, cons,
car and cdr are primitive. All other
procedures are potentially recursive and must take continuations.
(define p
(lambda (f a b)
(f
(letrec
([f (lambda (x)
(cond
[(null? x) '()]
[(pair? (car x)) (f (cons (f (car x)) (f (cdr x))))]
[else (cons (car x) (f (cdr x)))]))])
(f a))
b)))
Solution:
(define p
(lambda (f1 a b k1)
(letrec
([f2 (lambda (x k2)
(cond
[(null? x) (k2 '())]
[(pair? (car x))
(f2 (car x)
(lambda (v1)
(f2 (cdr x)
(lambda (v2)
(f2 (cons v1 v2) k2)))))]
[else
(f2 (cdr x)
(lambda (v3)
(k2 (cons (car x) v3))))]))])
(f1 a
(lambda (v4)
(f1 v4 b k1))))))
(+ 6
(call/cc (lambda (x)
(x (+ 5 (x (+ 3 4))))))
7)
Solution: 20
((call/cc (lambda (k)
(k (lambda (v)
((lambda (w) (+ w v))
v)))))
10)
Solution: 20
xerox takes a number n and
and item x and returns a list with n copies
of x.
(define xerox
(lambda (n x)
(if (zero? n)
'()
(cons x (xerox (sub1 n) x)))))
(xerox 3 'a) ==> (a a a)
(xerox 0 'a) ==> ()
xerox into continuation-passing style
by defining the procedure xerox-cps which takes a number
n, an item x, and a continuation k.
Redefine the procedure xerox so that it calls
xerox-cps with a proper initial continuation.
You may assume that the procedures zero?,
cons, and sub1 are primitive.
(define xerox-cps
(lambda (n x k)
(if (zero? n)
(k '())
(xerox-cps (sub1 n) x
(lambda (v)
(k (cons x v)))))))
(define xerox
(lambda (n x)
(xerox-cps n x (lambda (v) v))))
xerox-cps called xerox-ri, and represent
your continuations as records. Redefine xerox so that it
calls xerox-ri with a proper initial continuation. You
may assume "record.ss" has already been loaded.
Solution:
(define-record init-k ())
(define-record xerox-k (x k))
(define xerox-ri
(lambda (n x k)
(if (zero? n)
(apply-k k '())
(xerox-ri (sub1 n) x
(make-xerox-k x k)))))
(define apply-k
(lambda (k v)
(variant-case k
[init-k () v]
[xerox-k (x k)
(apply-k k (cons x v))])))
(define xerox
(lambda (n x)
(xerox-ri n x (make-init-k))))
xerox-ri so that it passes its arguments
in registers. Call the new program xerox-reg (which
should take zero arguments). Redefine xerox so that it
sets up the registers properly for the call to xerox-reg,
and returns the answer.
Solution:
(define-record init-k ())
(define-record xerox-k (x k))
(define *n #f)
(define *x #f)
(define *k #f)
(define *v #f)
; n x k
(define xerox-reg
(lambda ()
(if (zero? *n)
(begin
(set! *v '())
(apply-k))
(begin
(set! *k (make-xerox-k *x *k))
(set! *n (sub1 *n))
(xerox-reg)))))
; k v
(define apply-k
(lambda ()
(variant-case *k
[init-k () *v]
[xerox-k (x k)
(set! *k k)
(set! *v (cons x *v))
(apply-k)])))
(define xerox
(lambda (n x)
(set! *n n)
(set! *x x)
(set! *k (make-init-k))
(xerox-reg)))
(define prog
(lambda ()
(let ([x 1] [y 2])
(let ([f (lambda (a b)
(writeln a)
(writeln b)
(writeln x)
(writeln y)
(set! a (+ a b))
(writeln a)
(writeln x)
(set! a (+ a a))
(set! b (+ b b))
(writeln (+ a b x y)))])
(writeln x)
(writeln y)
(f x y)
(writeln x)
(writeln y)))))
Solution:
Value Reference Value-result 1 1 1 2 2 2 1 1 1 2 2 2 1 1 1 2 2 2 3 3 3 1 3 1 13 20 13 1 6 6 2 4 4
class Rent {
public void fries()
System.out.println("Cpl. Agarn");
return;
}
public void netfries()
this.fries();
return;
}
}
class Chillun extends Rent {
public void fries() {
System.out.println("Sgt. O'Rourke");
super.fries();
return;
}
}
What is printed out when the following block is evaluated?
{
Rent jane = new Rent();
Chillun parmenter = new Chillun();
jane.fries();
parmenter.fries();
jane.netfries();
parmenter.netfries();
}
Solution:
Cpl. Agarn Sgt. O'Rourke Cpl. Agarn Cpl. Agarn Sgt. O'Rourke Cpl. Agarn
loop-with-break.
loop-with-break takes a procedure p,
which takes one argument, break, and repeatedly
applies p to some procedure of no arguments which,
when invoked, will break out of the loop.
loop-with-break may return anything you like (it
returns the symbol *whatever* in the following
example).
> (let ((x 5))
(loop-with-break
(lambda (break)
(set! x (sub1 x))
(if (zero? x)
(break))
(displayln x))))
4
3
2
1
*whatever*
>
Solution:
(define loop-with-break
(lambda (p)
(call/cc
(lambda (k)
(letrec ([loop (lambda ()
(p (lambda () (k '*whatever*)))
(loop))])
(loop))))))
or
(define loop-with-break
(lambda (p)
(call/cc
(lambda (k)
(p (lambda () (k '*whatever*)))
(loop-with-break p)))))