home—info—lectures—exams—archive
hw-ec02
implementing objects: closures
Compare to
,
where we started with two random-number-generator functions,
and evolved them to an object-implementation (v4).
-
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.
-
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!'.
-
augment the above to return [a list containing] two methods:
get-val as described, and 'reset-password'. (Uses 'set!'.)
- 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.)
- Implement a sub-class:
create-single-use-lock
which works just like create-lock,
except that it ...
- Adds a new method ...
- 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.
home—info—lectures—exams—archive