7.8

Homework 5

home work!

Programming Language BSL

New Due Date: Saturday October 17, 6pm.

Purpose: Designing self-referential data and lists; designing functions that process self-referential data and lists.

Expectations

Failure to comply with these expectations will result in deductions and possibly a 0 score.

Note that this homework has been scaled back slightly because of the midterm exam. However, Homework 6 next week will start you off on the class project, and will be significantly more challenging.

Finger Exercises: HtDP/2e: 133, 138, 139, 141

Notes: to find the finger exercises, you’ll need to read the textbook and search for them by number. Also note the “2e” in the name: the exercise numbers changed substantially between the first and second editions. Make sure you’re reading the more recent edition. Finally, some of the finger exercises we recommend above rely on earlier exercises for context: you may need to scroll backwards in the textbook to figure out where the overall exercise starts.

Graded Exercises:

Exercise 1 A company called Pandora (no, not the music streaming service–a jewelry company) makes a line of customized charm bracelets. 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.

  • Design a self-referential data type for representing a Pandora-style charm bracelet. You should name your new data type CharmBracelet, and you should design it similar to how we designed the FundiesLab data type in class. We used that data type to string together a collection of TA staff members for a section; here, you are linking 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. Each non-terminal element, representing a single charm link, should be structured to hold several data fields:

    • a string description of the ornamental figure (e.g.: "Unicorn", "Double-Heart", "Skull-n-Bones" (wha???))

    • a field of data type Material, which you must define, and which will represent the three possible metals: silver, gold, or pewter

    • a self-referential field that represents the rest of the bracelet to which this charm is attached

    You should only have to define two new data types: Material and CharmBracelet; in fact, you are not allowed to define any other new data types.

  • Design the function bracelet-cost, that takes a CharmBracelet parameter 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.

Exercise 2
  • Design the data type FancyBracelet, where the links can be either:
    • a charm (described exactly as in Exercise 1); or

    • a colored bead, for which you will define a struct with color and size, which are a simple string and number, respectively.

    You can reuse your data type definition for Material from Exercise 1. Again, you should only have two data type definitions: this time, FancyBracelet and Material.

  • Design the function count-charms that takes a FancyBracelet, and counts the number of charms (and 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 with the requested figure. Any beads of other colors, as well as any current charms, should be left as-is.

Exercise 3 The following was the design for the Student data definition from Lecture 7:

(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 ListOfPosns (Prof. Park used "ListOfPositions") we did in class, design a data type for a ListOfStudents, building on the Student data type. In this exercise, you are building a real BSL list, so you should be using cons and 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.