You will be assigned a new partner for this homework; please complete it with your newly assigned partner.
Purpose
- To continue using recursive itemizations, including designing some functions that consume more than one recursive itemization.
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:
- No line may exceed 80 columns in length. How? Go to Racket Preferences > Editing > General Editing, and enable the “maximum character width guard.”
- The program must be properly indented, including the comments. How? Select some code and press Tab to automatically reindent just that code, or press Cmd/Ctl + I to reindent the whole file.
- Function and constant names must be descriptive, and argument names often should be.
- The program must follow the same naming conventions
as the textbook.
What?
For example, function names are
lower-kebab-case
, datatype names areUpper-kebab-case
orUpperCamelCase
, and constant names areSCREAMING-KEBAB-CASE
. In all style butUpperCamelCase
, words are separated by hyphens (-
).
Background
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:
- name
- sex
- date of birth
- fleece color
- dam (mother), if known
- sire (father), if known
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
;
; 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 AlpacaTree
s.
-
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
which contains Irene's name, her dam's name, and her granddam's name, and then stops because her great granddam is unknown.(list "Irene of Acorn Alpacas" "MFA Independence" "Dana Andrews")
Design the function
female-line
. (You probably need a new data definition in order to write the correct signature.) -
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. -
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.
Design a function
pedigree-error?
that returns true if a givenAlpacaTree
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
AlpacaTree
s are valid, in the sense thatpedigree-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’.) -
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 aAlpacaTree
).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.
-
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 asquicksort
andsort
.You will need a data definition for a list of
AlpacaTree
s (the conventional one will do). You will not need to write a whole sorting function for lists ofAlpacaTree
s, but you will probably need a helper functionmerge-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.) -
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.