Assignment 4

Due date: 2/9 @ 9:50 am

Problem 0 [5%]:

Find one interesting article from the weeks news on the use of software/computers in society. Summarize the article in your own words with a single 200-word paragraph, as a pair, one playing writer, the other playing editor. Add both the article and the summary in with the rest of your problem set.


Purpose:

The goal of this problem set is to practice designing data representations using a combination of structures and unions of classes and functions for processing these types of data. The graders will look for data definitions, contracts, purpose statements, examples/tests, and properly organized function definitions. For the latter, you must design templates. For this assignment you must have a template for each function you design.


HtDP Problem:

8.8.3 in the 2001 Second Printing edition

8.7.3 in the online version of the book


Problem 1:

The following data definition describes animals in a zoo:

An Animal is one of
-- Lion
-- Tiger
-- Bear

A Lion is (make-lion String Number)
(define-struct lion (name weight))

A Tiger is (make-tiger String  Boolean Number)
(define-struct tiger (name bengal? weight))

A Bear is (make-bear String Symbol Number)
(define-struct bear (name kind weight))
where kind is one of 'polar 'grizzly 'black 'brown 'kodiak
  1. Make at least two examples of each kind of animal.

  2. Design a template for the following function:
    ;; to answer a question about animals
    ;; fcn: Animal -> Answer
    (define (fcn ani) ...)
    
  3. Design a function that determines whether the animal weighs more than the average for its kind, when the average weight of lion, black bear, polar bear, and brown bear is 200 lbs, the average weight of kodiak, and grizzly bear is 300 lbs, the average weight of bengal tiger is 200, and the average weight of other kinds of tigers is 250.


Problem 2:

In lectures we used the following data definition to represent the information needed to build mobiles:

A Mobile is one of
-- Simple Mobile
-- Strut Mobile

A Simple Mobile is (make-simple Number Number)
(define-struct simple (cord weight))
where cord is the length of the cord on which the 
circle of the given weight hangs

A Strut Mobile is (make-mobile Number Number Mobile Number Mobile)
(define-struct mobile (cord lstrut lmobile rstrut rmobile))
where lstrut and rstrut are the lengths if the struts from which
the two other mobiles (lmobile and rmobile) hang
  1. Make three examples of mobiles - one with at least three levels. Make sure your examples are different from those used in lectures.

  2. Design a function that computes the total height of a mobile.

  3. Design a function that total lengths of all struts in a mobile.

  4. Design a function that determines whether a mobile is balanced. Remember that it means that at each level the total weight of the left side times the length of the left strut is the same as the total weight of the right side times the length of the right strut.

    Make sure to use helper functions when needed.


Problem 3:

Here is another data definition:

A Message is one of
-- String
-- Envelope

An Envelope is (make-env Message)
(define-struct env (msg))
  1. Make at least three examples of Messages.

  2. Design the function that counts the envelopes in a Message.

  3. Design the function that produces the String in a Message.


Problem 4:

Modify the definition of envelope to inlcude the information about the color of each envelope.

  1. Make at least three example of the new kinds of messages.

  2. Design the function that determines whether the message includes an envelope of some specified color.

  3. Challenge (optional): Design a function that produces the color of the envelope closes to the string of the message


Problem 5:

In this problem you are in charge of designing the data definitions. We will outsource the work of designing functions for your data to programmers overseas.

Our goal is to design a game where at each step a player is in a room with three doors leading to other rooms. (Of course, there is the also the door through which the player came - but he cannot look back or go back through that door, so it is not seen.)

Each of the three doors the player can see can be either open or closed. If the door is open, it leads to another room.

Each room contains some special object that will later determine how the game proceeds. For now, it will be just a word, such as "surprise", "gold", "danger", etc.

Eventually, the player ends up in a room with no more open doors.

Each room is unique - you can get to each room in only one way.

Design the data definition that represents the maze of rooms (ignore the player for now).

Make sure to include examples, and if possible, also a graphical representation of a small example.


Last modified: Mon Feb 5 17:00:56 EST 2007