CS U212 - Lab 4

As usual, the lab assignment is to be completed in pairs. Each pair works at one computer. Remember to trade roles!

Part I: Data Definitions Combining Unions and Structures

The following expressions can be represented by the data definition given below:

Data Definition:

;; An Expr is one of
;; -- 'go
;; -- 'stop
;; (make-expr Symbol Expr Expr)
;; where Symbol can only be 'both or 'either
(define-struct expr (op left right))

Exercise 1: Define the expressions shown above as Expr data. Make at least three additional examples of data. Then reverse the process and interpret your data as expressions similar to those shown above.

The rules for deciding whether the expression means 'go or 'stop are as follows:

Exercise 2: Determine the meaning of every one of your expressions (those given above and the ones you created in Exercise 1. Show the steps you had taken in finding the answer.

Exercise 3: Define the function meaning-both. It consumes two Symbols that can be either 'go or 'stop and produces the meaning of the corresponding 'both expression.

Exercise 4: Define the function meaning-either. It consumes two Symbols that can be either 'go or 'stop and produces the meaning of the corresponding 'either expression.

Exercise 5: Define the function meaning-expr that determines the meaning of an Expr.

Exercise 6: In the first three examples given at the beginning replace the four symbols 'both, 'either, 'go, and 'stop with the following four symbols: 'and, 'or, 'true, and 'false. What can you say about these examples?


Quiz


Part II: More Data Definitions

Consider the following data definition:
;; An Ancestor Tree (AT) is one of
;; -- 'unknown
;; -- (make-person String AT AT)
(define-struct person (name mother father))

Exercise 7: Make a data definition for Pete, whose mother is Ann, father is John, maternal grandparents are Jean and Dave, and paternal grandfather is Tim. He does not know the rest of his ancestors' names.

Exercise 8: Define the function count-names that counts how many people in the ancestor tree of a person have the given name (including himself or herself in the count).

Part III:Extras - The World

Hint: For the following exrcise represent the World as a struct with two Posns.

Exercise 9: Design a function mouse-click to react to mouse events. It consumes a World, two Numbers (x and y coordinates), and a MouseEvent, as described in on-mouse-event. When the MouseEvent is 'button-down, this function returns a new world wich consists of the first Posn of the original World and the x and y coordinates of the mouse click as the second Posn. Any other time, it produces the original World unchanged.

Exercise 10: Design a function tick-tock to react to clock events. The purpose of the function is to gradually equate two Posns. The function consumes a World and it produces a new World where the coordinates x and y of the first Posn are increased or dicreased by one so that they get closer to the coordinates of the second Posn of the original World. For example if the original World is (1,3),(5,1) then your function should return the new World (2,2),(5,1).

Exercise 11: Design a function world-draw that consumes a World and returns a 300-by-300 scene with a blue circle of radius 10 at the position represented by the first Posn of the world and a red circle of radius 10 at the position represented by the second Posn.

Exercise 12: Create an animation where you put a red circle somewhere in the canvas, using the mouse, and then a blue circle moves along the canvas trying to reach the red circle. The initial position of the blue circle is determined by the way you initialize the World.

Last modified: Thu Feb 1 23:38:47 EST 2007