next up previous contents
Next: DEFINING PROCEDURES IN Up: CHAPTER.4: PROCEDURES IN Previous: Properties as procedures

Exercise using properties for the rooms database

In chapter 1, an example was developed at length involving the dimensions of rooms, using a list of lists to represent all the information, in this format:

    vars rooms=
        [[room1 10 12 8]
         [room2 6 11 8]
         [room3 15 11 8]
         [room4 10 12 9]
         [room5 21 11 9]];
A disadvantage of this kind of representation is that if the number of items in the list is very large, then discovering the information about particular rooms may require lengthy searches down the list. Properties provide a far more efficient mechanism in such cases.

Try representing all this information using properties. Create a list of names of all the rooms:

    vars rooms = [ room1 room2 ...];
Then for each aspect of a room define a property or mapping from the room to its value for that aspect. E.g. room_length could be a property associating room names (e.g. "room1") with numbers (e.g. 10). Similarly room_breadth, room_height. Define these properties and set up their initial values. Then rewrite all the procedures from chapter 1 that previously searched through the list rooms so that they instead make use of these properties.

Another approach would be to have a single property called room_features, and associate each room with a list of three numbers, as in

    [6 11 8] -> room_features("room2");
Which method is best for which purposes, and why?



Aaron Sloman
Fri Jan 2 03:17:44 GMT 1998