{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

week5a_mutation_sol

# week5a_mutation_sol - Revenge of the Boxandpointers Q UESTI...

This preview shows pages 1–3. Sign up to view the full content.

CS61A Notes – Week 9: Muta4on (solu4ons) Revenge of the Box-and-pointers QUESTION We can also test if procedures are equal? . Consider this: > (define (square x) (* x x)) > (define (sqr x) (* x x)) > (eq? square sqr) => #f > (equal? square sqr) => #f It's obvious that square and sqr are not eq? . But they're also not equal? because for procedures, equal? does the same thing as eq? . Why can't we tell that square and sqr really do the same thing – and thus, should be “ equal? ”? (Since you guys are always paranoid, no, this won't be on the test.) The problem wants to check that square and sqr are “equal” in the sense that, given the same input, they always return the same output. This is impossible, and has been proven to be impossible! (You will see more of this in CS70). Therefore the most we can do is compare whether two things are the same procedure, not whether they’re the same function. QUESTION Draw box-and-pointer diagrams that show the structures v and w a±er evalua4ng those expressions. What does Scheme print for the values of v and w ? w => (d c b a) v => (a) Teenage Mutant Ninja. .. erm, Schemurtle (you try to do beYer) QUESTIONS 1. Personally – and don’t let this leave the room – I think set-car! and set-cdr! are useless; we can just implement them using set! . Check out my two proposals for set-car! . Do they work, or do they work? Prove me wrong: a. (define (set-car! thing val) b. (set! (car thing) val)) c. d. Doesn’t work – set! is a special form! It cannot evaluate what (car thing) is. a. (define (set-car! thing val) b. (let ((thing-car (car thing))) c. (set! thing-car val))) d.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
e. Doesn’t work. thing-car is a new symbol bound to the value of (car thing), and the set! statement simply sets the value of thing-car to val, without touching the original thing at all. 1. I’d like to write a procedure that, given a deep list, destruc4vely changes all the atoms into the symbol scheme : 2. > (define ls ‘(1 2 (3 (4) 5))) 3. > (glorify! ls) => return value unimportant 4. > ls => (scheme scheme (scheme (scheme) scheme)) 5. Here’s my proposal: 6. (define (glorify! L) 7. (cond ((atom? L) 8. (set! L ‘scheme)) 9. (else (glorify! (car L)) 10. (glorify! (cdr L)))))
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 6

week5a_mutation_sol - Revenge of the Boxandpointers Q UESTI...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online