On this page:
Data and Structure Defintions
Templates to Functions
Functions on Shapes
6.10

Lab 5: More and More Design

Implement this lab with the Beginning Student Language. Require the HtDP2e image and universe libraries at the top of your definitions:
(require 2htdp/image)
(require 2htdp/universe)

Make sure you follow The Style we use for the {B,I,A}SL{,+} languages in this class.

Choose the initial Head and Hands, and get started!

Data and Structure Defintions

Programs operate on data. For anything but the simplest data, built-in types of values are insufficient. Data definitions tell us how to create a certain type of value, while templates tell us how to tear apart that type of value.

Recall the data definition and template for Posns:

; A Posn is a (make-posn x y), where
; x is a Number and
; y is a Number.
 
; posn-template : Posn -> ???
(define (posn-template p)
  (... (posn-x p) ... (posn-y p) ...))

> (check-expect (posn-x (make-posn 1 2)) 1)

> (check-expect (posn-y (make-posn 1 2)) 2)

A Posn is a built-in data structure with the fields x and y. Note that the function make-posn accepts two values of any type. Per the above data definition, when we refer to Posn in function signatures or purpose statements, the x and y must be Numbers. These constraints are not enforced by the BSL; the data defintion is a contract that we programmers agree to obey while designing functions that make or use Posns.

The BSL provides the functions make-posn, posn-x, posn-y, and posn?. If these were not predefined, we could define our own Posn2 with define-struct:

(define-struct posn2 (x y))
; A 2dPosn is a (make-posn2 Number Number).
 
; posn2-template : 2dPosn -> ???
(define (posn2-template p)
  (... (posn2-x p) ... (posn2-y p) ...))
> (check-expect (posn2-x (make-posn2 1 2)) 1)
  (check-expect (posn2-y (make-posn2 1 2)) 2)

Note: the built-in operations on Posns have nothing to do with our Posn2s.

> (check-expect (posn-x (make-posn2 1 2)) 1)

posn-x: expects a posn, given (make-posn2 1 2)

And if we want to manipulate 3dPosns:

(define-struct posn3 (x y z))
; A 3dPosn is a (make-posn3 Number Number Number).
 
; posn3-template : 3dPosn -> ???
(define (posn3-template p)
  (... (posn3-x p) ... (posn3-y p) ... (posn3-z p)))

Ex 1: Read the BSL documentation for define-struct. In a comment, write down the names and signatures of all functions defined by the expression (define-struct posn3 (x y z)).

Hint: Most, but not all of the functions are shown in the above data definition and template for 3dPosns.

Ex 2: For any structure definition with N fields, how many functions are defined by define-struct? Write your answer in a comment.

Ex 3: Here are some data definitions:
(define-struct item (tag price))
; An Item is (make-item String PositiveNumber)
 
(define-struct TA (name field pay-rate))
; An TA is (make-TA String Field PositiveNumber)
; A Field is one of:
; - "biology"
; - "english"
; - "computer science"
; - "business"

Create at least two examples of values of each data definition.

Ex 4: Construct a template for functions that process Items.

Ex 5: Construct a template for functions that process TAs.

Templates to Functions

Swap Head and Hands!

Ex 6: Design a function pay-raise. It consumes two pieces of data: a TA and a number. The result is a new TA whose pay-rate is multiplied by the given number.

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

Functions on Shapes

Ex 8: A Shape may be either a box (a square), a pointy shape (a triangle), or a roundy shape (a circle). Design the data definitions box, pointy, roundy for Shapes, using structure definitions to represent squares, triangles, and circles. Each of these needs information about how big it is, what color it is, and what position it is on the screen. Make data examples for each kind of shape.

Ex 9: 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.

Ex 10: Design a function that, given an integer width, draws a Shape of that width on an empty scene. What will happen when you animate that function?