03-mutation-post3up

03-mutation-post3up - Mutation Readings HtDP section 39 and...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Mutation Readings: HtDP, section 39, and optionally sections 40, 41, 42. We will use a somewhat different approach to the ideas in these sections, and use some PLT Scheme features not discussed in the textbook. “As Yoda might say: Assignment leads to mutation. Mutation leads to pointers. Pointers lead to suffering!” (Anton van Straaten, PLT Scheme mailing list, 2001–12–24) CS 136 Winter 2009 03: Mutation 1 Loss of knowledge, again Accumulators allow us to retain knowledge from earlier parts of a recursive computation. But we can’t retain knowledge from earlier applications of a function, or from earlier applications of different functions. One place where this might be useful is in extended interaction with a user. Example: an address book. CS 136 Winter 2009 03: Mutation 2 Sample behaviour: > ( lookup " Arturo Belano " ) false > ( add-to-address-book " Arturo Belano " 6345789 ) > ( lookup " Arturo Belano " ) 6345789 We can’t ensure behaviour like this yet. CS 136 Winter 2009 03: Mutation 3 Mutation: the fall from grace The knowledge of prior history is called memory or state . In certain circumstances, we need to maintain state in variables, and change them during a computation. Scheme permits mutation , or rebinding of a name to a different value. ( define address-book empty ) ( set! address-book ( cons ’( " Arturo Belano " 6345789 ) address-book )) The effect is to rebind the name address-book to the new value ( cons ’( " Arturo Belano " 6345789 ) empty ) . It is as if the define expression had been rewritten. CS 136 Winter 2009 03: Mutation 4 ( define address-book empty ) ( set! address-book ( cons ’( " Arturo Belano " 6345789 ) address-book )) ⇒ ( define address-book ( cons ’( " Arturo Belano " 6345789 ) empty )) This idea of rewriting a definition is how we will handle the semantics of set! . CS 136 Winter 2009 03: Mutation 5 Variable variables address-book is a state variable whose value binding changes through mutation. Mutation can also be useful in improving efficiency. In a pure functional approach, mutation is not used. Scheme is not pure in this respect, but mutation is used only sparingly, when necessary. In contrast, mutation is central in imperative languages. CS 136 Winter 2009 03: Mutation 6 The equivalent of ( set! x ( + y z )) in C (and C++, and Java) is the assignment statement: x = y + z; which is typically covered at the very beginning of a first course, instead of a second course. As with I/O, the introduction of mutation complicates our efforts to understand what programs do. CS 136 Winter 2009 03: Mutation 7 We can now write the address book function. ( define ( add-to-address-book name phone ) ( set! address-book ( cons ( list name phone ) address-book )) The value of a set! expression is # < void > ....
View Full Document

This note was uploaded on 08/31/2011 for the course CS 136 taught by Professor Becker during the Fall '08 term at Waterloo.

Page1 / 14

03-mutation-post3up - Mutation Readings HtDP section 39 and...

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

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