6.7

Homework 2

home work!

Programming Language BSL

Due Date Wednesday 9/28 at 11:00 pm

Purpose To get experience designing programs that deal with unions, and then to begin working with recursion and lists.

For the purposes of this problem set, "design" means to use the Design Recipe to produce some requested function.

Exercise 1 A local Boston university hires you to help them build a system for keeping track of the members of its campus community. There are, principally, two kinds of people they want to model in their system: students and professors. For each student, they want to know the student’s first name, last name, major and expected year of graduation. For each professor, they want to know the professor’s first name, last name, department and salary.

Please design a data definition for a Person, that is, a professor or a student. You may need to define some accompanying structure types.

Give two examples of a Person: first, either yourself or your homework partner; and second, Prof. Shivers, who, for the purposes of this exercise, you may assume works in Computer Science and makes an annual salary of $100,000.

Finally, design a function that consumes a Person and produces an email greeting. For example, Prof. Shivers should get the greeting "Dear Prof. Shivers:". The university wants to address students in a friendly, more informal way, so student Pat Benson should get the greeting "Hey, Pat!"

Exercise 2 From HtDP, 134.

Exercise 3 Define a data definition, named LON, for lists of numbers.

Design the function any-even?. It determines whether or not the given LON contains an even number. Hint: Look up the even? function.

Exercise 4 Design the function total-length, which consumes a list of strings and computes their total length.

Exercise 5 Let’s return to your prior work for the Boston zoo (which you may recall from Lab 1). The Zoo keeps track of information for every animal that is kept there. For each animal, they store its name, species, number of legs, age, breakfast hour, and dinner hour. Here’s a data definition for a single animal:
(define-struct creature [name species legs age breakfast dinner])
 
; An Animal is a (make-creature String String Number Number String String)
Please provide a data definition for a LOA (a list of animals).

The state legislature has just passed a law taxing zoos on the basis of leg count. (The snake special-interest groups and lobbyists pushed the law through, over the protests of the arachnid enthusiasts.) To help compute the annual leg-tax owed by the zoo, design a function total-legs which takes a list of animals and produces the sum of the legs of all the animals in the list.

Correction: An early version of this problem set left the legs field out of the creature structure-type definition and the Animal data definition. We have fixed this.

Exercise 6 Design the function stackem/top-down, which consumes a list of images and produces an image containing all the input images, stacked vertically and centered horizontally. The first image in the list should be the highest image in the stack. Hint: See above. (Not the exercise above this one, but the function named above.)

Exercise 7 Design the function stackem/bottom-up, which consumes a list of images and produces an image containing all the input images, stacked vertically and centered horizontally. The first image in the list should be the lowest image in the stack. Hint: See above. (Not the function named above, but the exercise above this one.)

Exercise 8 Design the function assemble. It consumes a list of strings and produces an image. The latter shows all the strings in order, rendered as texts of size 22 in blue. The first string in the list should be the leftmost part of the result image.