Honors-section homework
You have to work on this problem set with your new partner.
You may use Intermediate Student Language (ISL) for this assignment.
You must follow the design recipe and the guidelines when you develop
functions. We suggest you use helper functions liberally to improve
the readability of your code.
Exam Grades
Your professors and TAs have been grading Exam 1. We were each
assigned a pile of exams to grade and when we're done grading we turn
in a list of exam grades for each of the students in our pile.
Here is the data definition for exam grades and lists of exam grades:
(define-struct examgrade (student points comment))
;; An ExamGrade is a (make-examgrade String Number String)
;; A LOEG (list of exam grades) is one of:
;; - empty
;; - (cons ExamGrade LOEG)
Each exam grade records the name of the student, the student's
points on the exam, and a comment from the grader.
Exercise 1:
Design a function, merge-ordered, that takes two lists of
exam grades as input. Both input lists are ordered by the points
scored on the exam, from highest to lowest. Your function should
combine the two lists into one list of exam grades that is also in
descending order by number of points.
Exercise 2: Professor Ahmed has just been given two lists
of exam grades for the students in the honors section. The first list
contains students' grades for the regular exam, while the second
contains grades for the honors supplement. She wants you to design a
function, honors-totals, that takes these two lists as
input and produces a list of exam grades that contains an entry for
each student with the student's total number of points (regular exam +
honors supplement). Both input lists are alphabetically sorted by
student name and the output list should also be alphabetically sorted.
The two input lists, however, may not be of the same length (we
sometimes misplace exams!). We may
have a regular-exam grade for a student but no honors-supplement
grade, or vice versa. In such cases, that student's entry in the
output list should have the points for whichever grade is available
(regular exam or honors supplement), together with the comment
"Missing regular" or "Missing honors", as
appropriate. Finally, all comments in the input lists should be
dropped.
Here are some examples (but you should develop more of your own):
(honors-totals
(list (make-examgrade "Amal Ahmed" 58 "Aced it!")
(make-examgrade "Olin Shivers" 30 "Not so good"))
(list (make-examgrade "Amal Ahmed" 44 "Wow!")
(make-examgrade "Olin Shivers" 25 "")))
--->
(list (make-examgrade "Amal Ahmed" 102 "")
(make-examgrade "Olin Shivers" 55 ""))
(honors-totals
(list (make-examgrade "Amal Ahmed" 58 "Aced it!")
(make-examgrade "Olin Shivers" 30 "Not so good"))
(list (make-examgrade "Leena Razzaq" 38 "")
(make-examgrade "Olin Shivers" 25 "")))
--->
(list (make-examgrade "Amal Ahmed" 58 "Missing honors")
(make-examgrade "Leena Razzaq" 38 "Missing regular")
(make-examgrade "Olin Shivers" 55 ""))
Legos!
Here are data definitions for lego bricks and lego buildings:
(define-struct lego (label color width))
;; A Lego is a (make-lego Number Symbol Number)
(define-struct bigger (lego left right))
;; A LegoBldg (lego building) is one of:
;; - Lego
;; - (make-bigger Lego LegoBldg LegoBldg)
Each lego brick has a label, color, and width (in
pixels). We construct a lego building out of lego bricks, and make
bigger buildings by putting a lego brick on top of two lego buildings
(left and right).
Exercise 3:
Design a function, count-bricks, that takes a lego
building and produces the total number of lego bricks in the
building.
Exercise 4:
Each lego brick is 10 pixels tall.
Design a function, how-high, that takes a lego building
and produces the total height of the lego building (in pixels).
Exercise 5:
Design a function, contains-colored-brick?, that takes a
lego building and a color, and determines whether the building
contains a lego brick of the given color.
Exercise 6:
Design a function, find-colored-brick?, that takes a lego
building and a color and finds any lego with the given color in the
building, or returns false if there are no such legos.
Here is a data definition for the type of data this function returns:
;; A MaybeLego is one of:
;; - false
;; - Lego
Your function should not use contains-colored-brick?, it
should not traverse/examine parts of the building more than once, and it
should stop searching once any brick of the given color is found.
Exercise 7:
Design a function, lego->image, that takes a lego and
produces an image of the lego. All legos are rectangular and 10
pixels tall.
Design a function, lb->image, that takes a lego
building and produces an image of the building. (You may want to look
up above and beside/align in Help Desk.)
Here are some examples:
(make-bigger (make-lego 4 'purple 80)
(make-bigger (make-lego 2 'blue 60)
(make-lego 1 'yellow 40)
(make-lego 3 'red 40))
(make-bigger (make-lego 6 'orange 60)
(make-lego 5 'green 40)
(make-lego 7 'red 40)))
(make-bigger (make-lego 4 'purple 80)
(make-bigger (make-lego 2 'blue 60)
(make-lego 1 'yellow 40)
(make-lego 3 'red 40))
(make-lego 6 'orange 60))
Exercise 8:
Design a function, merge-lb, that takes two lego
buildings and merges them into a new lego building. Two buildings can
be merged if corresponding bricks (starting with the brick at the
top) are of the same color. If the buildings cannot be merged, the
function should produce an error: merge-lb: Colors don't
match. The two buildings need not have the same number of
bricks; the bricks in each building that don't correspond to bricks in
the other building should be discarded.
Here's how two legos la and lb are
merged (assuming their colors match):
- If
la is labeled with the number m and
lb is labeled with the number n, then the
lego we get by merging them is labeled with the number
m.n.
-
The legos
la, lb, and the merged lego piece
all have the same color.
-
The widths of
la and lb are added together to
get the width of the merged piece.
Here are some examples to illustrate:
(merge-lb
(make-bigger (make-lego 2 'blue 60)
(make-lego 1 'yellow 40)
(make-lego 3 'red 40))
(make-bigger (make-lego 2 'blue 60)
(make-lego 1 'yellow 40)
(make-lego 11 'red 20)))
--->
(make-bigger (make-lego 2.2 'blue 120)
(make-lego 1.1 'yellow 80)
(make-lego 3.11 'red 60))
(merge-lb
(make-bigger (make-lego 4 'purple 80)
(make-bigger (make-lego 2 'blue 60)
(make-lego 1 'yellow 40)
(make-lego 3 'red 40))
(make-lego 6 'orange 60))
(make-bigger (make-lego 4 'purple 80)
(make-lego 2 'blue 60)
(make-bigger (make-lego 6 'orange 60)
(make-lego 5 'green 40)
(make-lego 7 'red 40))))
--->
(make-bigger (make-lego 4.4 'purple 160)
(make-lego 2.2 'blue 120)
(make-lego 6.6 'orange 120)))
Orderly Legos
Let's build a different kind of lego building where all legos
in the building must have distinct labels and brick labels
appear in a certain order in the building.
Here is the data definition for our ordered lego buildings:
;; An OrdLegoBldg (ordered lego building) is one of:
;; - 'none
;; - Lego
;; - (make-bigger Lego LegoBldg LegoBldg)
;; where each lego brick in the building has a unique label and
;; where each (make-bigger l lft rgt) has the property that
;; -- the label of l is bigger than all the labels in lft
;; -- the label of l is smaller than all the labels in rgt
All functions that you design below must exploit/maintain the
distinct-label and label-ordering properties of
OrdLegoBldgs.
Exercise 9:
Design a function, ord-lb-contains?, that takes an
ordered lego building and a label and determines whether the building
contains a lego with that label.
Exercise 10:
Design a function, colors-in-order, that takes an
ordered lego building and produces a list of colors. The output list
must be ordered so that it starts with the color of the lego with the
smallest label in the building and ends with the color of the lego
with the highest label.
You may use append to concatenate lists of colors. The
function should not traverse/examine parts of the building more than once.
Exercise 11:
Design a function, grow-ord-lb, that takes an
ordered lego building and a lego brick and adds the brick to the
building. If the new brick's label is the same as the label of a
brick already in the building, return an error: Label already in
building. The new building must be an OrdLegoBldg, that is, it
must respect the distinct-label and label-ordering properties of
OrdLegoBldg.
Exercise 12:
Design a function, build-ord-lb, that takes a list of
legos and produces an ordered lego building built using these legos.
The building must be an OrdLegoBldg (i.e., it must
satisfy the distinct-label and label-ordering properties of
OrdLegoBldg.). If the input list contains multiple legos
with the same label, report an error.
X-Expressions
Here is the data definition for X-expressions:
#| X-expressions:
Xexpr is one of:
-- (cons Symbol (cons LOA Xbody))
-- (cons Symbol Xbody)
Xbody is one of:
-- empty
-- (cons String Xbody)
-- (cons Xexpr Xbody)
LOA is one of:
-- empty
-- (cons (list Symbol String) LOA)
Note: (list Symbol String) is called an Attribute.
Furthermore, (list 'a "some text") is called an a-Attribute
and "some text" is its value.
|#
X-expressions are used to represent XML information within the BSL
language
The "teachpack" more-io.ss provides the
function read-xexpr for reading an entire XML files as an
X-expression. To use the teachpack, save it in the same directory as
your code for the problem set. In DrRacket, select the Language >
"Add Teachpack..." menu item, then "Add Teachpack to
List...", then navigate to the location of more-io.ss
and add it.
Before submitting, any use of read-xexpr must be
commented out.
Here is a sample XML piece:
<week>
<assignment>
hello
<syllabus week="2" src="sample2.xml" />
<syllabus week="1" src="sample1.xml">
world
</syllabus>
</assignment>
done
</week>
Exercise 13: Represent it as an X-expression to practice your data representation skills.
Exercise 14:
Your task is to design four functions:
-
find-srcs, which consumes the name of an XML file (a
file whose extension is .xml and whose content is an XML
expression) and produces the list of all the
values of src attributes in the file.
-
xexpr-find, which consumes an X-expression and
produces the list of all the values of src attributes.
-
file-depth, which determines the depth, also called
complexity, of the XML in the file of the given name.
If you have at most one start tag per line and if you indent one
more space every time you use an open tag without closing
preceding tags, the depth of a piece of XML is the maximal number
of spaces between a tag and the left border.
-
xexpr-depth, which consumes an X-expression and
determines its depth.
You should notice that two of the functions are one-line definitions,
if you use function composition to define them.
For your entertainment, this assignment comes with the XML file
6h.xml, which stores information about this
assignment. You may use it to run your program (not test). For
testing, you must create a simpler file than this.
Exercise 15: The last problem required the design of the
functions: xexpr-find
and xexpr-depth. Because the description of the input
requires three data definitions, those of you who designed the
functions according to the design recipe came up with three mutually
interconnected functions (plus perhaps some auxiliary functions).
The first step to editing such clusters of
functions after they have been properly designed and corrected is to
re-organize them into a single function, just like the problem
statement (or your boss's request) states for both of the two
functions in this problem. Do so.
The reason for organizing code in this manner is to create an
organizational structure for future
readers. If xexpr-find or xexpr-depth occur
in the context of a large module or program that some reader wishes to
understand, he may just read the purpose statement and ignore the
internal nest of local functions during a first pass. This provides a
first overview of the module and puts everything in context. After
that, the reader can "drill" down wherever needed.
Exercise 16: XML experts refer to (the interpretation of) an Xexpr as an
element. Design the function xexpr-elements, which
consumes an X-expression x and symbol t and
extracts a list of all elements whose tag is t. Note that
an element with tag t may occur nested inside of some
other element with the same tag.
Exercise 17: Abstract
over xexpr-find, xexpr-depth, and
xexpr-elements. That is, create a single function that
consumes and processes an Xexpr and demonstrate that the three
functions are simple "instances" of this function.
Hint: Start from the template for all three functions. (Remember that
since all three functions process the same kind of input data their
templates---i.e., their organizational skeletons---are identical. Then
create a function that consumes appropriate "combinators" and "base
values" for the various clauses that must be distinct.
Knowledge: This abstraction is at the core of many XML processing
languages and thus shows up at the heart of many XML libraries.