On this page:
From Structure Type Definitions to Data Definitions
Templates
From Templates to Functions
Moving Shapes
6.10.1

Lab 3: Structures and Enumeration

From Structure Type Definitions to Data Definitions

Remember structure type definitions from class:

(define-struct name (field ...))

What do structure type definitions do?

Exercise 1 List the functions that the following sample definitions define:

(define-struct photo (image tag))
(define-struct 3d (x y z))

Recall that a data definition for structures explains using both English and Beginning Student how to construct elements of this class of data. In particular, it specifies what kind of data each field contains. Refer to Section 5.4 of the textbook if you need to review.

Exercise 2 Here are some data definitions:

(define-struct item (tag price))
; An Item is (make-item String PositiveNumber)
 
(define-struct AI (name field pay-rate))
; An AI is (make-AI String Field PositiveNumber)
; A Field is one of:
;  "biology"
;  "english"
;  "computer science"
; - "business"

Create at least two data examples per data definition.

Exercise 3 The Exotic Feline Rescue Center keeps track of information for every cat that is kept there. For each cat, they store its name, species, age, and feeding time (if they don’t get fed on schedule, they try to eat the visitors...).

Define a structure type Cat for representing information about an exotic feline.

Now formulate a data definition for your structure type definition.

Templates

What are templates? Again, see Section 5.4 of HtDP2e if you forgot.

Exercise 4 Construct a template for functions that process Items.

Exercise 5 Construct a template for functions that process AIs.

Exercise 6 Construct a template for functions that process Cats.

From Templates to Functions

Let’s start with a couple of exercises on designing functions that work on the above structure types.

Exercise 7 Design the function pay-raise. It consumes two pieces of data: an AI and a number. The result is a new AI whose pay-rate is multiplied by the given number.

Exercise 8 Design the function bonus. It consumes one piece of data: an AI. The result is a number which is twice the pay rate if the AI is in English or Biology, three times the pay rate if the AI is in Business, and four times the pay rate if the AI is in computer science.

Hint Remember the design recipes for all the different kinds of data we’ve seen this week.

Moving Shapes

In lecture, we’ve seen big-bang programs with animation. Now we will develop one which moves a shape of your choice across the screen.

Exercise 9 A Shape could be either a box (a square), a pointy shape (a triangle), or a round shape (a circle). Each of these needs information about how big it is, what color it is, and what position it is on the screen.

Design a data definition that can represent any of these three kinds of Shapes. Make data examples for each kind of shape.

Exercise 10 Design a function that takes a Shape as input and produces a picture where the shape has been drawn on a white background. Use your data examples in the examples when designing this function.

Exercise 11 Design a function that moves a shape to a new place. You can choose what direction to move the shape, and how far to move it.

Exercise 12 Use the functions you just designed to create an animation with big-bang that moves a shape across the screen.