2.6 Objects and Nested Objects
Behind this here mask
Lies a mystery: an enigma
A secret is nesting
2.6.1 Objectives
what is meant by a nested structure
create a nested structure
write a function to process a nested structure
2.6.2 Exercises (due 2011/09/28 08:45:00)
1. A person consists of a name, age, address and telephone number. An address consists of a street, city, state, and zip code. Write the data definitions for person and address. Then write the function provoSeniorHuh which returns true if the given person is a senior (65 or older) and lives in the zip code 84604.
2. Define a structure that keeps track of the cat’s (x,y) coordinate and its happiness (expressed as a percentage number between 0 to 100.) Write a function called feed that consumes a cat and produces a cat with an increased happiness by 10%.
3. Write a cat-to-cat function called newspaper that decreases a cat’s happiness by 15.
4. Write a cat-to-cat function called jump that increases a cat’s y position by 10.
5. Define a structure called zoo that contains two cats. Write feedLeft and feedRight that consume a zoo and return a zoo where the left (right) cat has been fed with the feed function.
6. Write newspaperLeft and newspaperRight
7. Write jumpLeft and jumpRight
8. Modify jumpLeft and jumpRight to ensure that when cat’s jump, they can’t land on each other. Assume that cats are perfect circles with radius 5. If a jump would have a cat land another, leave them both unmoved.
2.6.3 Optional Exercises (due 2011/10/03 08:45:00)
9. Write a cat-to-cat function called celestialize that returns a perfectly happy cat at the same position.
10. Write celestializeLeft and celestializeRight
11. Add an attribute to cats called color which contains their fur color as a string. Write dye, cat-to-cat function, which returns a cat with a new color.
12. Write dyeLeft and dyeRight
2.6.4 Notes
These notes are primarily for my sake, but I don’t see any reason to hide them from you.
Nested structure |
|
Posn => |
Ball with vert/horiz velocity |
Ball with both velocity |
|
tick : ball -> ball |
|
Phone (area,digits) => |
Telephone book entry (cell,home,office) |
|
all-in-area? : bentry area => boolean |
|
The template step grows to include calling other functions to do the job |
|
box (ul and lr posn) |
-- translate |