RU beehive logo ITEC dept promo banner
ITEC 380
2008fall
ibarland

homeinfolecturesexamsarchive

hw-ec02
implementing objects: closures

Compare to , where we started with two random-number-generator functions, and evolved them to an object-implementation (v4).

  1. Write a scheme method 'create-lock' which takes a value and a password; it returns function (perhaps namable "get-val") which takes a purported-password and returns *either* the original value, or false if the password didn't match.
  2. after three bad attempts in a row, it 'self-destructs' -- that is, not even the correct password will work any more. This requires that the object keep track of history, for which our example used 'set!'.
  3. augment the above to return [a list containing] two methods: get-val as described, and 'reset-password'. (Uses 'set!'.)
  4. implement method-dispatch: After the second extra-cred, modify 'create-lock' so that it again returns a single function, a "lock-object": If you pass the lock-object 'get-val and a string, it calls the get-val function described earlier. If you pass the lock-object 'reset-password and two strings, it instead calls the reset-password function described earlier. (This requires var-args.)
  5. Implement a sub-class: create-single-use-lock which works just like create-lock, except that it ...
    1. Adds a new method ...
    2. Overrides the existing method get-val So that after a single successful lookup (i.e. a lookup which doesn't return false), it never works again.

homeinfolecturesexamsarchive


©2008, Ian Barland, Radford University
Last modified 2008.Nov.07 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme