EECS 111: Homework 5

Due: Tuesday, May 14 at 11:59 PM

You will be assigned a new partner for this homework; please complete it with your newly assigned partner.

Purpose

Design & Code Style

Design and code style are both essential to effective programming, and thus each will be graded. For design, we expect you to follow the six-step Design Recipe. For style, it means that:

Background

alpaca! The Alpaca Owners and Breeders Association needs your help! They’re having trouble using the database of detailed pedigree records that they keep for all registered alpacas.

For every alpaca in the registry, they keep several different pieces of information:

Unsurprisingly, AOBA uses DrRacket to store and query the alpaca registry, with the following data definitions:

; An AlpacaTree is one of:
;  - (make-alpaca String Sex Date Color AlpacaTree AlpacaTree)
;  - "unknown"
(define-struct alpaca [name sex dob color dam sire])
;
; where two alpacas
;
; A Sex is one of:
;  - "female"
;  - "male"
;
; A Date is (make-date Year Month Day)
(define-struct date [year month day])
;   where
; Year is an Integer in [1900, 2019]
; Month is an Integer in [1, 12]
; Day is an Integer in [1, 31]
An an example, here’s the representation of the record for Irene of Acorn Alpacas:
(define IRENE
  (make-alpaca "Irene of Acorn Alpacas"
               "female"
               (make-date 2007 5 21)
               "silver"
               (make-alpaca "MFA Independence"
                            "female"
                            (make-date 2002 7 2)
                            "black"
                            (make-alpaca "Dana Andrews"
                                         "female"
                                         (make-date 1996 8 14)
                                         "silver"
                                         "unknown"
                                         "unknown")
                            (make-alpaca "Jericho de Chuchata"
                                         "male"
                                         (make-date 1997 11 23)
                                         "black"
                                         "unknown"
                                         "unknown"))
               (make-alpaca "MA Sylvan"
                            "male"
                            (make-date 2001 5 16)
                            "black"
                            "unknown"
                            "unknown")))

Your Task

Before starting the assignment, be sure to write down the template for processing AlpacaTrees.

  1. alpaca! First, AOBA would like a program to make a rather simple query: Given an alpaca, they would like to find out the names of all the female-line ancestors of the given alpaca in a list, youngest to oldest, and including the given alpaca (regardless of the sex of the given alpaca). So for example, given the structure for Irene above, it should return the list

    (list "Irene of Acorn Alpacas"
          "MFA Independence"
          "Dana Andrews")
    which contains Irene's name, her dam's name, and her granddam's name, and then stops because her great granddam is unknown.

    Design the function female-line. (You probably need a new data definition in order to write the correct signature.)

  2. Many breeders raise alpacas for their fleece, which comes in a wide variety of colors and may be made into a wide variety of textiles. Some breeders are interested in breeding alpacas with new colors and patterns, and to do so, they need to understand how fleece colors and patterns are inherited.

    You can help them by designing a function has-color? that takes an alpaca pedigree tree and a color, and reports whether or not that color appears anywhere in the pedigree tree.

  3. AOBA is worried about fraud in their registry. Eventually they’ll send investigators into the field, but first they’d like to run a consistency check on the database. Given the pedigree tree for an alpaca, there are two kinds of errors that you can detect between alpacas and their parents:

    • Some alpaca in the tree has a birthday before one of its parents.
    • A male alpaca is listed as a dam, or a female alpaca is listed as a sire.

    alpaca! Design a function pedigree-error? that returns true if a given AlpacaTree contains one of those two obvious errors, and false otherwise. Hint It’s easy to tie yourself in recursive knots with this one. One trick to stay out of trouble is to avoid “stacked selectors” like these: (alpaca-sex (alpaca-sire a)). Templates for recursive itemizations do one level of structural decomposition and leave the rest to recursion; if you feel a need to look deeper but not recursively, that’s a sign that you need a helper function. Stylish solutions to this problem may involve as many as four or five helpers. .

    (For all other problems in this assignment, you should assume that all AlpacaTrees are valid, in the sense that pedigree-error? answers false for them. In particular, your code for the next two problems should be simpler than it otherwise could because you don’t have to consider the possibility that any alpaca’s date of birth could precede its parents’.)

  4. Tracing back an alpaca’s ancestry as far as possible is a point of pride in the alpaca-raising community. Design a function oldest-ancestor that, given an alpaca’s pedigree tree, returns its oldest known ancestor (also as a AlpacaTree).

    If neither the dam nor the sire is known, then an alpaca is its own oldest ancestor. Programmers call this a degenerate or trivial case.

  5. AOBA also wants a way to list all the known ancestors of an alpaca (including the given alpaca) in reverse birth order. For example, for Irene, the result would be a list starting with Irene’s, followed by MFA Independence (DOB 7/2/2002), MA Sylvan (DOB 5/16/2001), Jericho de Chuchata (DOB 11/23/1997), and Dana Andrews (DOB 8/14/1996).

    Design a function all-ancestors/sorted to perform this task without using the built-in sorting functions such as quicksort and sort.

    You will need a data definition for a list of AlpacaTrees (the conventional one will do). You will not need to write a whole sorting function for lists of AlpacaTrees, but you will probably need a helper function merge-alpacas that, given two sorted lists of alpaca pedigree trees, merges them into a single sorted list of alpaca pedigree trees. (See HtDP §23.5 for how to design a template for this.)

  6. Bonus: Design an alpaca pedigree explorer. This should display pedigree information on an alpaca and allow the user to call up the information on other alpacas in a way you consider appropriate. (For example, clicking the alpaca’s dam might call up her information.) Be as creative as you like.

Turn In

Please write all your code in one .rkt file and submit it through Canvas.