lab8 - The second problem is that the value remembered in...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 CS 61AWeek 8 solutions LAB: 1. Add REPEAT to person class. (define-class (person name) (INSTANCE-VARS (OLD-TEXT '())) (method (say stuff) (SET! OLD-TEXT STUFF) stuff) (method (ask stuff) (ask self 'say (se '(would you please) stuff))) (method (greet) (ask self 'say (se '(hello my name is) name))) (METHOD (REPEAT) OLD-TEXT)) Notice that the ASK and GREET methods don't have to set old-text, because they use the SAY method. 2. Which double-talkers work? (define-class (double-talker name) (parent (person name)) (method (say stuff) (se (usual 'say stuff) (ask self 'repeat))) ) There are two things wrong with this approach. One is that it assumes that the two arguments to SE are evaluated left-to-right, since the use of REPEAT assumes that we've just said the new stuff. That might work in some versions of Scheme but not in others.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The second problem is that the value remembered in old-text will be a single copy of the argument, rather than two copies therefore, if we ask this double-talker to repeat, it'll only say the thing once. (define-class (double-talker name) (parent (person name)) (method (say stuff) (se stuff stuff)) ) This would work except for the REPEAT feature. We can ask a double-talker to ASK or to GREET and it will correctly say the right thing twice. But if we ask this double-talker to REPEAT, it won't say anything at all, because it never remembered the stuff in old-text. (define-class (double-talker name) (parent (person name)) (method (say stuff) (usual 'say (se stuff stuff))) ) This one works as desired....
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online