cs61a lecture 18
play

CS61A Lecture 18 Amir Kamil UC Berkeley March 4, 2013 - PowerPoint PPT Presentation

CS61A Lecture 18 Amir Kamil UC Berkeley March 4, 2013 Announcements HW6 due on Thursday Trends project due tomorrow Ants project out Persistent Local State A function with a parent frame The parent contains local state Every call


  1. CS61A Lecture 18 Amir Kamil UC Berkeley March 4, 2013

  2. Announcements  HW6 due on Thursday  Trends project due tomorrow  Ants project out

  3. Persistent Local State A function with a parent frame The parent contains local state Every call changes the balance Example: http://goo.gl/5LZ6F

  4. Non ‐ Local Assignment def make_withdraw(balance): """Return a withdraw function with a starting balance.""" def withdraw(amount): Declare the name "balance" nonlocal nonlocal balance if amount > balance: return 'Insufficient funds' Re ‐ bind balance balance = balance - amount where it was return balance bound previously return withdraw

  5. Mutable Values and Persistent State

  6. Mutable Values and Persistent State Mutable values can be changed without a nonlocal statement.

  7. Mutable Values and Persistent State Mutable values can be changed without a nonlocal statement. Example: http://goo.gl/cEpmz

  8. Mutable Values and Persistent State Mutable values can be changed without a nonlocal statement. Name ‐ value binding cannot change Example: http://goo.gl/cEpmz

  9. Mutable Values and Persistent State Mutable values can be changed without a nonlocal statement. Mutable value can change Name ‐ value binding cannot change Example: http://goo.gl/cEpmz

  10. Creating Two Withdraw Functions Example: http://goo.gl/glTyB

  11. Multiple References to a Withdraw Function Example: http://goo.gl/X2qG9

  12. The Benefits of Non ‐ Local Assignment   

  13. The Benefits of Non ‐ Local Assignment  Ability to maintain some state that is local to a function, but evolves over successive calls to that function.

  14. The Benefits of Non ‐ Local Assignment  Ability to maintain some state that is local to a function, but evolves over successive calls to that function.  The binding for balance in the first non ‐ local frame of the environment associated with an instance of withdraw is inaccessible to the rest of the program.

  15. The Benefits of Non ‐ Local Assignment  Ability to maintain some state that is local to a function, but evolves over successive calls to that function.  The binding for balance in the first non ‐ local frame of the environment associated with an instance of withdraw is inaccessible to the rest of the program.  An abstraction of a bank account that manages its own internal state.

  16. The Benefits of Non ‐ Local Assignment  Ability to maintain some state that is local to a function, but evolves over successive calls to that function.  The binding for balance in the first non ‐ local frame of the environment associated with an instance of withdraw is inaccessible to the rest of the program.  An abstraction of a bank account that manages its own internal state. Weasley Account $10

  17. The Benefits of Non ‐ Local Assignment  Ability to maintain some state that is local to a function, but evolves over successive calls to that function.  The binding for balance in the first non ‐ local frame of the environment associated with an instance of withdraw is inaccessible to the rest of the program.  An abstraction of a bank account that manages its own internal state. Weasley Potter Account Account $10 $1,000,000

  18. Referential Transparency

  19. Referential Transparency Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program.

  20. Referential Transparency Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program. mul(add(2, mul(4, 6)), 3)

  21. Referential Transparency Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program. mul(add(2, mul(4, 6)), 3) mul(add(2, 24 ), 3)

  22. Referential Transparency Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program. mul(add(2, mul(4, 6)), 3) mul(add(2, 24 ), 3) mul( 26 , 3)

  23. Referential Transparency Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program. mul(add(2, mul(4, 6)), 3) mul(add(2, 24 ), 3) mul( 26 , 3) Mutation is a side effect (like printing)

  24. Referential Transparency Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program. mul(add(2, mul(4, 6)), 3) mul(add(2, 24 ), 3) mul( 26 , 3) Mutation is a side effect (like printing) Side effects violate the condition of referential transparency because they do more than just return a value; they change the state of the computer.

  25. Referential Transparency Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program. mul(add(2, mul(4, 6)), 3) mul(add(2, 24 ), 3) mul( 26 , 3) Mutation is a side effect (like printing) Side effects violate the condition of referential transparency because they do more than just return a value; they change the state of the computer.

  26. Referential Transparency Expressions are referentially transparent if substituting an expression with its value does not change the meaning of a program. mul(add(2, mul(4, 6)), 3) mul(add(2, 24 ), 3) mul( 26 , 3) Mutation is a side effect (like printing) Side effects violate the condition of referential transparency because they do more than just return a value; they change the state of the computer.

  27. A Mutable Container

  28. A Mutable Container def container(contents):

  29. A Mutable Container def container(contents): """Return a container that is manipulated by two functions.

  30. A Mutable Container def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello')

  31. A Mutable Container def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get()

  32. A Mutable Container def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello'

  33. A Mutable Container def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world')

  34. A Mutable Container def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get()

  35. A Mutable Container def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world'

  36. A Mutable Container def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world' """

  37. A Mutable Container def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world' """ def get():

  38. A Mutable Container def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world' """ def get(): return contents

  39. A Mutable Container def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world' """ def get(): return contents def put(value):

  40. A Mutable Container def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world' """ def get(): return contents def put(value): nonlocal contents

  41. A Mutable Container def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world' """ def get(): return contents def put(value): nonlocal contents contents = value

  42. A Mutable Container def container(contents): """Return a container that is manipulated by two functions. >>> get, put = container('hello') >>> get() 'hello' >>> put('world') >>> get() 'world' """ def get(): return contents def put(value): nonlocal contents contents = value return put, get

  43. Dispatch Functions

  44. Dispatch Functions A technique for packing multiple behaviors into one function

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend