On this page:
How to Design a Function
Positions
6.10

Lab 4: Design and Composites

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!

How to Design a Function

In your previous labs we’ve asked you to define various functions. From now on, we’re going to ask you to design a function. You must follow the design recipe to design a function.

Before you begin any implementation, you should have written down any relevant data definition, a function signature, header, and purpose statement, as well as input/output examples covering both common and edge cases.

; what-temp : Number -> String                                  <- Signature
; Given a temperature (in degrees Fahrenheit), return a string  <- Purpose
; string description of the comfort level of that temperature.     Statement
(define (what-temp df) "")                    ; <- Stub
(check-expect (what-temp 32)       "cold")    ; <- Examples/Tests
(check-expect (what-temp 45.0)     "comfy")
(check-expect (what-temp 68)       "comfy")
(check-expect (what-temp 74.99999) "comfy")
(check-expect (what-temp 75)       "hot")
(check-expect (what-temp 100)      "hot")

Note: From now on, the first response you get from any TA will be "What step of the design recipe are you on?"

Ex 1: Design a function simulated-ta that, given any string, returns the proper TA response: "What step of the design recipe are you on?" Before asking any questions in this or future labs, first ask your simulated-ta.

Positions

Programs manipulate data. We describe how to create data with data defintions and describe how to tear apart data for use in functions with templates.

Consider Posns–a composite data structure provided by BSL:

; A Posn is a (make-posn x y), where
; x is a Number and
; y is a Number.

A Posn represents a two-dimensional position. The data definition describes how to make a Posn: give the function make-posn two numbers, such as (make-posn x y). We can pass that value (make-posn x y) to any function that expects a Posn as its input.

This template describes the data available in any function that expects a Posn as its input:

; posn-template : Posn -> ???
(define (posn-template p)
  (... (posn-x p) ... (posn-y p) ...))
; where (posn-x (make-posn x y)) == x
;   and (posn-y (make-posn x y)) == y.

While each of the following functions have different signatures, purpose statements, stubs, and examples/tests (function-specific parts of the design recipe), the Posn data definition and template remain the same (data-specific parts of the design recipe).

Ex 2: Design a function posn-distance that given two posns, returns the scalar distance between the two posns.

Ex 3: Design a function place-circle that consumes a posn and produces a 300×300 scene with a red circle of radius 10 at the position represented by the posn. For instance, if given (make-posn 10 290) it should produce a scene with a red circle in the lower left corner.

Swap Head and Hands.

Ex 4: Design a function in-circle? that consumes two posns and a positive integer a. The first posn may be any point, while the second represents the center of a circle with the area a. The function in-circle? should return #true only if the first point is inside the circle, and #false otherwise.

Hint: You may want to define a helper-function area->radius in your solution to exercise 4.

Ex 5: Design a function in-rectangle? that consumes two posns and two positive integers w, h. The first posn may be any point, while the second represents the center of a rectangle with the width w and height h. The function in-rectangle? should return #true only if the second point is inside the rectangle, and #false otherwise.