notes10s - 1 CS61ANotes10NowTheMutantsAttack[Solutionsv1.0]

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

View Full Document Right Arrow Icon
CS61A Notes 10 – Now The Mutants Attack [Solutions v1.0] That Which Look The Same May Not Be The Same (Thy eyes are devil’s idle playthings) (weird) 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? ”? 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 . Teenage Mutant Ninja…  err, Schemurtle (you try to do better) QUESTIONS 1. Personally, I think  set-car!  and  set-cdr!  are pretty useless too; 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 by drawing box-and-pointer diagrams. a. (define (set-car! thing val) (set! (car thing) val)) Doesn’t work – set! is a special form! It cannot evaluate what (car thing) is. b. (define (set-car! thing val) (let ((thing-car (car thing))) (set! thing-car val))) 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. 2. I’d like to write a procedure that, given a deep list, destructively changes all the atoms into the  symbol  chung : > (define ls ‘(1 2 (3 (4) 5))) > (glorify! ls) ==> return value unimportant > ls ==> (chung chung (chung (chung) chung))  Here’s my proposal: (define (glorify! ls) (cond ((atom? ls) (set! ls ‘chung)) (else (glorify (car ls)) (glorify (cdr ls))))) Does this work?  Why not?  Write a version that works. Chung Wu; CS61A, Spring 2004 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
No. Remember, to manipulate elements of a list, you need to use set- car! or set-cdr!. set! sets ls to ‘chung when ls is an atom, but once we return, the new value for ls is lost. Here’s a way to do this: (define (glorify! ls) (cond ((null? ls) ‘()) ((atom? ls) ‘chung) (else (set-car! ls (glorify! (car ls))) (set-cdr! ls (glorify! (cdr ls))) ls))) We need to return ls because the set-car! and set-cdr! expressions expect glorify! to return something – namely, the transformed sublist. 3.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

notes10s - 1 CS61ANotes10NowTheMutantsAttack[Solutionsv1.0]

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online