hw9 - CS 61A - Week 10 Solutions LAB ASSIGNMENT: 3.12...

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

View Full Document Right Arrow Icon
CS 61A -- Week 10 Solutions LAB ASSIGNMENT: 3.12 append vs. append! exp1 is (b); exp2 is (b c d). Append (without the !) makes copies of the two pairs that are part of the list x. (You can tell because it uses cons, which is the constructor function that generates a brand new pair.) Append! does not invoke cons; it mutates the existing pairs to combine the two argument lists. 2. Set! vs. set-cdr! There are two ways to think about this, and you should understand both of them: The syntactic explanation -- SET! is a special form; its first argument must be a symbol, not a compound expression. So anything of the form (set! (. ..) . ..) must be an error. The semantic explanation -- SET! and SET-CDR! are about two completely different purposes. SET! is about the bindings of variables in an environment. SET-CDR! is about pointers within pairs. SET! has nothing to do with pairs; SET-CDR! has nothing to do with variables. There is no situation in which they are interchangeable. (Note: The book says, correctly, that the two are *equivalent* in the sense that you can use one to implement the other. But what that means is that, for example, if we didn't have pairs in our language we could use the oop techniques we've learned, including local state variables bound in an environment, to simulate pairs. Conversely, we'll see in Chapter 4 that we can write a Scheme interpreter, including environments as an abstract data type, building them out of pairs. But given that we are using the regular Scheme's built-in pairs and built-in environments, those have nothing to do with each other.) 3a. Fill in the blanks. > (define list1 (list (list 'a) 'b)) list1 > (define list2 (list (list 'x) 'y)) list2 > (set-cdr! ____________ ______________) okay > (set-cdr! ____________ ______________) okay > list1 ((a x b) b) > list2 ((x b) y) The key point here is that if we're only allowed these two SET-CDR!s then we'd better modify list2 first, because the new value for list1 uses the
Background image of page 1

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

View Full DocumentRight Arrow Icon
sublist (x b) that we'll create for list2. So it's (set-cdr! (car list2) (cdr list1)) (set-cdr! (car list1) (car list2)) 3b. Now do (set-car! (cdr list1) (cadr list2)). Everything that used to be "b" is now "y" instead: > list1 ((a x y) y) > list2 ((x y) y) The reason is that there was only one appearance of the symbol B in the diagram, namely as the cadr of list1; every appearance of B in the printed representation of list1 or list2 came from pointers to the pair (cdr list1). The SET-CAR! only makes one change to one pair, but three different things point (directly or indirectly) to it. 3.13 make-cycle The diagram is +----------------+ | | V | ---> XX--->XX--->XX---+ | | | V V V a b c (last-pair z) will never return, because there is always a non-empty cdr to look at next. 3.14
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 / 17

hw9 - CS 61A - Week 10 Solutions LAB ASSIGNMENT: 3.12...

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