Functional Problem Solving (CSC 151 2014F) : Labs

Lab: Objects in Scheme


Summary: In this lab, we consider techniques for building objects, collections of data that support operations on those data.

Exercises

Exercise 1: A Multi-Stage Switch

As you may know, some switches have more than two positions. For example, we might have a switch that switches between the values 0, 1, 2, and 3. (For a two-way light, 0 might represent “off”, 1 might represent “light one on”, 2 might represent “light two on”, and 3 might represent “both lights on”.) In general, you can only toggle to the next higher value (with 3 returning to 0 when toggled).

Implement a four-stage-switch object that responds to the same messages as a switch, but with four possible stages.

Exercise 2: A Single Tally Object

Define a one-field object, tally, that responds to exactly four messages:

  • ':get-value, which returns the value of the tally;
  • ':set-contents-to-zero!, which resets the tally to 0 (that is, the next call to ':get-value should return 0);
  • ':increment!, which has the effect of increasing the number stored in the contents field by 1; and
  • ':decrement!, which has the effect of decreasing the number stored in the contents field by 1.

The initial value of the tally should be 0.

Note that you are creating a single object, not a procedure that creates objects.

For example,

> (define tally ...)
> (tally ':get-value)
0
> (tally ':increment!)
> (tally ':get-value)
1
> (tally ':decrement!)
> (tally ':decrement!)
> (tally ':decrement!)
> (tally ':get-value)
-2

Exercise 3: Making Generic Tallys

a. Define a make-tally procedure that constructs and returns objects similar to the tally object you defined in the previous exercise.

b. Create two tally objects and demonstrate that they can be incremented and reset independently.

Exercise 4: Tallys with Initial Values

Write a new make-tally procedure that allows the client to create new tallys with a specied initial value. For example, a professor might say that a starting grade is 90 with

> (define grade (make-tally 90))

The professor would then increment and decrement it as students do good or bad work.

Exercise 5: Monitored Tallys

a. Define a constructor procedure, make-monitored-tally, for objects similar to the tally objects from above, except that each such object keeps track of the total number of messages that it has received.

Hint: For this exercise, you may want to make two vectors, one for the value of the tally and one for the count of operations. Alternately, you could make a two-element vector in which element 0 of that vector is the value of the tally and element 1 of that vector is the count of operations.

b. Test your procedure.

For Those with Extra Time

Extra 1: Parametric Messages

We saw in the reading how to create objects that accept messages with parameters. For instance, turtles need to know how far forward to move. Rather than a tally that requires the user to call it several times to affect a change of greater than one, we may want to change the tally by a certain amount.

Define a constructor procedure, make-mutable-tally, for objects similar to the tally objects from above, except that each object can take an additional message, :change-by! that has the effect of changing the current value of the tally by an argument given to the object.

For example,

> (define mtally (make-mutable-tally))
> (mtally ':get-value)
0
> (mtally ':increment!)
> (mtally ':get-value)
1
> (mtally ':change-by! 5)
> (mtally ':get-value)
6
> (mtally ':change-by! -2)
> (mtally ':get-value)
4

Extra 2: Turtles

You likely have seem by now that turtles are implemented as objects in MediaScheme. You know enough now to build your own implementation. Write a turtle constructor (make-turtle image) that takes an image identifier as a parameter, keeps track of its position and orientation, and responds to the following messages:

  • ':pen-up and ':pen-down
  • ':turn
  • ':forward

Note that you need to use some geometry to calculate a new position (x',y') from the distance ds, current position (x,y), and angle a: x'=x+d*cos(a) and y'=y+d*sin(a).

If you still have more time, add ':teleport and ':set-color messages. The latter is subtle, you may not have noticed, but while turtles draw with their own color, they leave the GIMP in the state they found it, which means they need to get and save the current foreground color, change the color (for drawing a line), and restore the GIMP foreground color.