module5-print

module5-print - Module 5 Mutation and imperative languages...

Info iconThis preview shows pages 1–3. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Module 5 Mutation and imperative languages Because sometimes things change CS 116: Introduction to Computer Science 2 Daniel G. Brown/Troy Vasiga, University of Waterloo 5.1 Purpose of Module 5 Mutation: changing variables Imperative languages: a different kind of programming Mutation of structures Readings: HtDP 34, 35, 36, 40.3, 40.5, 41.1, 41.2; PfSD 1, 2 5.2 1 Mutation: changing variables Some variables never change A rule we have used in 116 and in 115: Any function always returns the same value when called with the same arguments. ( define x 120) ( sqr x ) 14400 .... absolutely anything at all .... ( sqr x ) 14400 Our functions definitions cannot change. Our variables definitions cannot change. So we evaluate the same expression for the functions, and the values of the functions argu- ments will have the same value. Nothing changes. 5.3 No mutation makes life easier This is very comforting: ( define x 120) ...( f x )... x No matter what f is, even if we didnt write it , even if we cant see it: f cant change the value of x . It will always be 120. 5.4 1 1.1 Why have mutation? This isnt always what we want However, there are times that this might not seem so helpful. A natural example might be a route finding program ( define localmap (... something-complicated ...)) ( find-route localmap uwaterloo conestogamall ) (( university to phillip ) ( phillip to columbia ) ( columbia to king ) ( king to destination )) This is fine, until times change. 5.5 But things change! This works fine until: Theres construction that blocks a road, or Someone builds a new road. In the way that we have had so far, if one call to ( find-route localmap uwaterloo conestogamall ) gives a given route, every other call will as well. We cant re-map the route; its impossible. 5.6 Other reasons to have different outcomes There might be other times wed want some way of having two different calls to the same function return different things. Example 1 . For example, our current model wont work at all with input or output. Imagine a function get-key-from-keyboard . How can the same function call return "a" when someone pushes that key and "q" when someone pushes that one? 5.7 Or a game Or imagine a board game, where we want to be able to go forward a turn: ( define board ...) ( show-current-board board ) ( play-a-turn board ) ( show-current-board board ) We have to be able to change the state of the board, or we cant play a boardgame! (Or build a document in a word processor, or ...) 5.8 This cant work! This kind of code cant do what we want: Both calls to show-current-board must have identical values....
View Full Document

This note was uploaded on 07/26/2011 for the course CS 116 taught by Professor T.vasiga during the Spring '09 term at Waterloo.

Page1 / 13

module5-print - Module 5 Mutation and imperative languages...

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