61A Lecture 12Monday, September 26Monday, September 26, 2011Implementing Dice2http://www.math.utah.edu/~pa/Random/Random.htmlMonday, September 26, 2011Implementing DiceRandom numbers are useful for experimentation2http://www.math.utah.edu/~pa/Random/Random.htmlMonday, September 26, 2011Implementing DiceRandom numbers are useful for experimentationThey also appear in lots of algorithms, e.g.,2http://www.math.utah.edu/~pa/Random/Random.htmlMonday, September 26, 2011Implementing DiceRandom numbers are useful for experimentationThey also appear in lots of algorithms, e.g.,•Primality tests2http://www.math.utah.edu/~pa/Random/Random.htmlMonday, September 26, 2011Implementing DiceRandom numbers are useful for experimentationThey also appear in lots of algorithms, e.g.,•Primality tests•Machine learning techniques2http://www.math.utah.edu/~pa/Random/Random.htmlMonday, September 26, 2011Implementing DiceRandom numbers are useful for experimentationThey also appear in lots of algorithms, e.g.,•Primality tests•Machine learning techniques2http://www.math.utah.edu/~pa/Random/Random.htmlMonday, September 26, 2011Implementing DiceRandom numbers are useful for experimentationThey also appear in lots of algorithms, e.g.,•Primality tests•Machine learning techniques2def make_dice(sides=6): seed = 1 def dice(): nonlocal seed seed = (16807 * seed) % 2147483647 return seed % sides + 1 return dicehttp://www.math.utah.edu/~pa/Random/Random.htmlMonday, September 26, 2011Implementing DiceRandom numbers are useful for experimentationThey also appear in lots of algorithms, e.g.,•Primality tests•Machine learning techniques2def make_dice(sides=6): seed = 1 def dice(): nonlocal seed seed = (16807 * seed) % 2147483647 return seed % sides + 1 return dice231 - 1http://www.math.utah.edu/~pa/Random/Random.htmlMonday, September 26, 2011Implementing DiceRandom numbers are useful for experimentationThey also appear in lots of algorithms, e.g.,•Primality tests•Machine learning techniques2def make_dice(sides=6): seed = 1 def dice(): nonlocal seed seed = (16807 * seed) % 2147483647 return seed % sides + 1 return dice231 - 1http://www.math.utah.edu/~pa/Random/Random.htmlMonday, September 26, 2011Implementing DiceRandom numbers are useful for experimentationThey also appear in lots of algorithms, e.g.,•Primality tests•Machine learning techniques2def make_dice(sides=6): seed = 1 def dice(): nonlocal seed seed = (16807 * seed) % 2147483647 return seed % sides + 1 return dice231 - 1S.K. Park and K.W. Miller, " Random Number Generators: Good Ones Are Hard To Find", Communications of the ACM, October 1988, pp. 1192-1201.http://www.math.utah.edu/~pa/Random/Random.htmlMonday, September 26, 2011Referential Transparency, Lost3Monday, September 26, 2011Referential Transparency, Lost• Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.3Monday, September 26, 2011Referential Transparency, Lost• Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.3mul(add(2, mul(4, 6)), add(3, 5))Monday, September 26, 2011Referential Transparency, Lost• Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.3mul(add(2, mul(4, 6)), add(3, 5))mul(add(2, 24 ), add(3, 5))Monday, September 26, 2011Referential Transparency, Lost• Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.3mul(add(2, mul(4, 6)), add(3, 5))mul(add(2, 24 ), add(3, 5))mul( 26 , add(3, 5))Monday, September 26, 2011Referential Transparency, Lost• Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.3• Re-binding operations violate the condition of referential transparency because they let us define functions that do more than just return a value; we can change the environment, causing values to mutate.mul(add(2, mul(4, 6)), add(3, 5))mul(add(2, 24 ), add(3, 5))mul( 26 , add(3, 5))Monday, September 26, 2011Referential Transparency, Lost• Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.3• Re-binding operations violate the condition of referential transparency because they let us define functions that do more than just return a value; we can change the environment, causing values to mutate.mul(add(2, mul(4, 6)), add(3, 5))mul(add(2, 24 ), add(3, 5))mul( 26 , add(3, 5))Monday, September 26, 2011Referential Transparency, Lost• Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.3• Re-binding operations violate the condition of referential transparency because they let us define functions that do more than just return a value; we can change the environment, causing values to mutate.mul(add(2, mul(4, 6)), add(3, 5))mul(add(2, 24 ), add(3, 5))mul( 26 , add(3, 5))Monday, September 26, 2011Referential Transparency, Lost• Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.3• Re-binding operations violate the condition of referential transparency because they let us define functions that do more than just return a value; we can change the environment, causing values to mutate.mul(add(2, mul(4, 6)), add(3, 5))mul(add(2, 24 ), add(3, 5))mul( 26 , add(3, 5))DemoMonday, September 26, 2011Implementing a Mutable Container Object4def make_container(contents): def get(): return contents def put(value): nonlocal contents contents = value return get, putget, put = make_container('Hi')Monday, September 26, 2011Implementing a Mutable Container Object4def make_container(contents): def get(): return contents def put(value): nonlocal contents contents = value return get, putget, put = make_container('Hi')make_container(contents):make_container:...Monday, September 26, 2011make_container('Hi')Implementing a Mutable Container Object4def make_container(contents): def get(): return
View Full Document