Homework 5
Due Date: Wednesday February 12, 9pm
Purpose To design simple list processing functions; practice using hierarchical data
Expectations
You should submit three .rkt files, each containing your response to one of the 3 exercises below, via the Handin Server; the three files should be named "HW5-1.rkt", "HW5-2.rkt", and "HW5-3.rkt". We accept NO email submissions. Failure to submit a .rkt file will result in a 0 for that part.
You are only allowed to use the language specified at the top of this page: failure to do so will result in a 0.
Your code must conform to the guidelines outlined in the style guide on the course website. Pay particular attention to the list of prohibited functions in the guide. The style guide may be updated as the semester progresses so please remember to read it before submitting each assignment.
Unless otherwise stated, all data and functions you design must be designed according to the data and function design recipes (DR), resp., following all four steps in each case, with the exception of functions that call big-bang, for which you need no check-expects, as justified in class. All functions used inside big-bang must have check-expects, along with the other three steps of the design recipe.
We will not walk you through the DR steps in each problem, nor request them individually and explicitly. Instead, review the data and function design recipes before starting this homework, and then have these recipes handy.
-----------------------------------------------------------------------------
This week, we learned about self-referential data types–structure data types that have as one of their fields a reference to more of its own type (recall the Flight and System data type definitions). We applied the concept to creating a simulation for a planet that had multiple moons (we were aiming for Jupiter or Saturn, which each have over 80 moons!). Using self-referential structures, we were able to accomodate an unlimited number of moons for a system, unlimited number of passengers on a flight, etc..
However, each data type definition was designed from scratch, with custom define-structs, custom selectors, unique base cases, and so on.
We subsequently learned about a common infrastructure in BSL–lists– that allowed us to generalize the handling of these types of collections using a uniform representation: a chain of cons cells terminating in a special "empty list", '(). We no longer had custom constructors and selectors, like make-system and system-moon, instead using common functions like cons and first. We also streamlined our data type definitions in the process.
In this assignment, you will do the first two exercises using the custom define-structs (i.e., no cons or '()). Then, for the last exercise, you will use the new-style cons/'() form.
Exercise 1 Bracelets with Charm A company called Pandora (no, not the music streaming service-–a jewelry company) makes a line of customized charm bracelets. A charm bracelet consists of a series of jewel links, each one carved into an interesting figure or shape. You buy individual charms that come in all sorts of interesting shapes, to commemorate special moments in your life. You link these charms together, add a clasp at the end, and voila–a very personalized bracelet. (Note that when worn, you would of course close the clasp to form a circular bracelet, but for this assignment, think about it in the open, "linear chain of links" form, having a beginning and and end.)
First, design the data type Charm representing an individual charm link, with the following data fields:
a description of the ornamental figure (e.g.: "Unicorn", "Double-Heart", "Skull-n-Bones"); this is an open-ended set, so you should not use an enumeration for this, just a simple String)
a field of data type Material, which you must define, representing one of the three possible metals charms are made of: silver, gold, or pewter
Next, design the self-referential data type CharmBracelet, for representing a Pandora-style charm bracelet. You must do this in the way we first designed self-referential data types: with custom define-structs, and base cases of your own choosing, like #false. For this exercise, you must not use cons or '(), (nor list or any other function or concept we haven’t covered in lecture up to this point).
Your data definition should allow you to link together a chain of Charms into a bracelet. Follow the usual pattern for such self-referential data types: each "charm" should be a structure of some sort, and the sequence of charms in the bracelet should terminate in some special type of value, representing the clasp at the end.
Design the function bracelet-cost, that takes a CharmBracelet and returns the sale price for the bracelet, based on a cost of $15 for each gold charm, $12 for each silver charm, and $10 for each pewter charm. Create at least 4 interesting test cases for this function.
Note that for this exercise, You should only have to design three data types: Material, Charm, and CharmBracelet; in fact, you are not allowed to define any other new data types.
Exercise 2 Fancier Bracelets For this exercise, you will upgrade your bracelet to be able to link together not only charms, but also colored beads, in any mix and order.
Design the data type FancyBracelet, which is an upgraded version of CharmBracelet, where now the links can be of two types; either:
a Charm (defined exactly as in Exercise 1); or
a colored Bead, for which you will design a new data type, with fields to represent color and size, which are a simple string and number, respectively.
You should reuse your data type definitions for Material and Charm from Exercise 1. Make sure you copy-and-paste the definitions from your solution for Exercise 1 into your solution for this part, so that all the necessary definitions for this problem are in one place. Then, for this exercise, you should have two new additional data type definitions, for FancyBracelet, and Bead. Added: It is permissible to define an additional data type if needed.
Design the function count-charms that takes a FancyBracelet, and counts only the number of charms, not beads, on the bracelet.
Design the function upgrade-bracelet that takes three parameters: a FancyBracelet, a bead color, and a charm figure, and exchanges all of the beads of the given color in the bracelet for silver charms of the desired figure. Any beads of other colors, as well as any current charms, should be left as-is.
Exercise 3 A Student Database The following is the design for a Student data definition:
(define-struct student [firstname lastname gpa on-coop]) ; A Student is a (make-student String String Number Boolean) ; Interpretation: A (make-student fn ln g c) represents a ; Northeastern student whose first name is fn and last name is ln, with ; cumulative grade point average g, and for whom c is #true if they are ; currently doing a coop experience this term and #false otherwise. (define student1 (make-student "Jane" "Smith" 4.0 #true)) (define student2 (make-student "Ashok" "Singhal" 0.0 #false)) (define (student-templ st) (... (student-firstname st) ... (student-lastname st) ... (student-gpa st) ... (student-on-coop st) ...))
Using the same kind of design as for the ListOfPositions we did in class, design the data type ListOfStudents, building on the Student data type given above. In this exercise, you are building a real BSL list, so you should be using cons and '() (or empty) to build up your list.
Design the function count-coop-students that takes a ListOfStudents, and returns the number of students who are currently doing their coop experience.
Design the function exchange-coop-students that takes a ListOfStudents and flips each Student’s coop status, so that students who are currently doing a coop will now be listed as back at school, and students who are currently not doing a coop will now be listed as doing a coop. (In other words, toggle the coop status field.) You are expected to implement all simple boolean simplifications in doing this actual flip.