Write your name in the top right-hand corner of this page. There are 100 points total. You have the entire class period for this exam. Show your work to receive partial credit. If you use any procedure that is not in standard Scheme, define it (in any blank space that seems large enough). Check your work carefully!
mark-depth
that takes a nested list structure and replaces
every occurrence of the symbol depth by the number of lists
that it is contained in.
(mark-depth '(depth 1 ((2 depth) #f) depth)) ==> (1 1 ((2 3) #f) 1)
ANSWER:
(define mark-depth
(lambda (nlst)
(letrec ((mark (lambda (nls depth)
(cond
[(null? nls)
'()]
[(list? (car nls))
(cons (mark (car nls) (add1 depth))
(mark (cdr nls) depth))]
[(eq? (car nls) 'depth)
(cons depth (mark (cdr nls) depth))]
[else
(cons (car nls) (mark (cdr nls) depth))]))))
(mark nlst 1))))
a. (let ((x 1)
(y 10))
(+ (begin (set! x 2)
(let ((x (* y 2))
(y (+ 5 x)))
(+ x y)))
x))
ANSWER: 29
b. (let ((x 1))
(let ((set!x (lambda (value) (set! x value))))
(let ((x (* x 4)))
(set!x 5)
x)))
ANSWER: 4
c. (let ((x 1))
(let ((set!x (lambda (value) (set! x value))))
(let ((x (* x 4)))
(set!x 5))
x))
ANSWER: 5
d. (let ([x 1])
(let ([f (lambda (y) (+ x y))]
[g (delta (y) (+ x y))])
(let ([x 2])
(+ (f x) (g x))))))
ANSWER: 7
e. (let ((x 1))
(let ((f (lambda (y) (+ x y))))
(let ((g (delta (z) (+ x (f z)))))
(let ((x 10))
(g 5))))))
ANSWER: 16
f. (let ((x 1))
(let ((f (lambda (y) (+ x y))))
(let ((g (lambda (z) (+ x (f z)))))
(let ((x 10))
(g 5))))))
ANSWER: 7
g. (let ((x 1))
(let ((f (delta (y) (+ x y))))
(let ((g (delta (z) (+ x (f z)))))
(let ((x 10))
(g 5))))))
ANSWER: 25
(let ((x 5))
(let ((y (+ x 1))
(f (lambda (a) (lambda () (+ y x)))))
(let ((z y))
(* z ((f 3))))))
ANSWER: FV = {+,y,*}
max. lexical depth = 2
(let ((x 5))
(let ((y ([+: free] [x: 0] 1))
(f (lambda (a) (lambda () ([+: free] [y: free] [x: 2])))))
(let ((z [y: 0]))
([*: free] [z: 0] (([f: 1] 3))))))
(letrec ((y (+ x 1))
(x 2))
y)
ANSWER:
(let [(x 'ignore) (y 'ignore)]
(set! x 2)
(set! y (+ x 1))
y)
If another Scheme implementation tries to evaluate the value of y
before it assigns the value of x, then this expression might not work.
ANSWERS: expressed values = num + char + ptr(cell(expressed value)) + ptr(procedure) denoted values = procedure + cell(expressed value)
<exp> -> <number>
| (<operator> <exp> ...)
<operator> -> + | - | * | /
Your program begins with
(load "datatype.ss")
(define-datatype arith-exp
(num (value number?))
(exp (operator operator?)
(operands (list-of arith-exp?))))
Define recordify, evaluate, and any other
procedures necessary to obtain an implementation of this simple language,
with the usual meaning of expressions and numbers being any Scheme number.
(evaluate (recordify '(* 2 (+ 3 (/ 6 2))))) ==> 12
ANSWER:
(define operator?
(lambda (sym)
(memv sym '(+ - * /)))
(define list-of
(lambda (pred)
(lambda (lst)
(andmap pred lst))))
(define recordify
(lambda (e)
(cond
[(number? e)
(make-num e)]
[(operator? (car e))
(make-exp (car e) (map recordify (cdr e)))]
[else
(error 'expression "Invalid arithmetic expression syntax")])))
(define evaluate
(lambda (myexp)
(type-case arith-exp myexp
[(num val)
val]
[(exp rator rands)
(apply (eval rator) (map evaluate rands))])))