=================
Problem 1
MCE> (if (= 2 2) 3 4)
3
We redefine true in MCE, not Scheme.
=================
Problem 2
MCE> quote
(compound-procedure quote ...)
MCE> (quote 10)
10
(quote-exp? exp) is before (application? exp) in the big cond of mc-eval.
=================
Problem 3
(define useless-square
(let ((val #f))
(lambda (x) (if val
val
(begin (set! val (* x x)) val)))))
=================
Problem 4
You should draw environment diagram for these.
(bar 1)
Lexical: 10
Dynamic: 20
foo
Lexical: 20
Dynamic: 100
=================
Problem 5
part 1
MCE: error
Analyzing: error
Lazy: value 7
MCE dynamic: error
part 2
All errors
=================
Problem 6
First one is faster in analyzing because it contains if and recursive call.
Second one is the same.
=================
Problem 7
(rule (rotate-forward (?x . ?y) ?z)
(append ?y (?x) ?z))
(rule (rotate-backward ?x ?y)
(rotate-forward ?y ?x))
=================
Problem 8
(define (tree-member? x tree)
(if (eq? x (datum tree))
#t
(forest-member? x (children tree))))
(define (forest-member? x forest)
(cond ((null? forest) #f)
((tree-member? x (car forest)) #t)
(else (forest-member x (cdr forest)))))
=================
Problem 9
(define (num-sum exp)
(cond ((number? exp) exp)
((atom? exp) 0) ; note that () is also atom
(else (+ (num-sum (car exp)) (num-sum (cdr exp))))))
=================
Problem 10
a) L is still (a b c)
b) L is (a c)
c) L is still (a b c). The call to remove-first also results in error.
=================
Problem 11
> (define f (foo 1))
> (f 2)
(3 6 (2 4) (1 4))
; follow through, not restarted
> (define g (foo 2))
> (g 1)
(4 5 (3 5 4) (2 5 4))
> (f 2)
(5 6 (4 3 5 7) (1 7))
> (g 1)
(6 5 (5 4 3 8 7) (2 8 7))
If you're not sure, use envdraw to check. Note that (cons (+ a 1) L) should point the cdr of the new pair to L, not copy L over.
=================
Problem 12
(define-class (job description work-to-do)
(class-vars (num-jobs 0))
(instance-vars (id 0))
(initialize (set! id num-jobs)
(set! num-jobs (+ 1 num-jobs)))
(method (done?) (<= work-to-do 0))
(method (do-work amount)
(if (<= work-to-do 0)
'(but you are already done!)
(set! work-to-do (- work-to-do amount)))))