CMSC15100 Autumn 2010: Homework 2

Due Tuesday, October 12 at 10pm.

Setup

This homework requires the use of the image.ss teachpack, so you will need to include the following at the beginning of your program:

(require 2htdp/image)

Note that you should not add the image.ss teachpack using the Language|Add Teachpack... menu, since that will load an older version that has a different interface.

Documentation for the image.ss teachpack is available at use image.ss or from the Help|Help Desk menu (look at the HtDP/2e Teachpacks).

Exercise 1: Distance between two points

Develop a function

;; distance : posn posn -> num
;; returns the distance between p1 and p2
;; (define (distance p1 p2) ...)

Exercise 2: Mixed data

A shape is either a circle, defined by its center (a posn) and radius, or a rectangle, defined by its upper-left corner (a posn) and its width and height (integers). Give definitions for shapes and develop a function

;; render : shape mode color -> image

that renders a shape as an image using the given drawing mode and color.

NOTE: that you will need to use the names "Circle" and "Rectangle" (note leading uppercase letter) for the struct names, since "circle" and "rectangle" will give you an error that the name already has a built-in meaning.

Exercise 3: Body mass

A person's body mass index (BMI) is defined to be their mass (in kilos) divided by their height (in meters) squared. Define a data representation for a person that includes three fields: name, mass, and height. Then define a function for computing a person's BMI.

Exercise 4: Lists of people

Using the data definition from Exercise 3 and the following definition for lists of people

;; a list of people is either
;;  - empty
;;  - (cons person list-of-people)

write a function tallest that returns the tallest person from a list of people.

Exercise 5: Lists of numbers

Given the following data definition for lists of numbers:

;; a list of numbers is either
;;  - empty
;;  - (cons num list-of-numbers)

Write the following recursive functions:

;; len : list-of-numbers -> num
;; return the length of the list l
;; (define (len l) ...
;; sum : list-of-numbers -> num
;; return the sum of the list of numbers
;; (define (sum l) ...
;; sq-list : list-of-numbers -> list-of-numbers
;; return a list of the squares of the numbers in the list l
;; (define (sq-list l) ...