Please note: This assignment is to be completed with your new assigned partner
Purpose
The goal of this assignment are to use structure and itemization data, following the appropriate design recipe, and to practice designing data representations
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 (-
).
Problems
Evaluate the following expressions step by step and write in a comment next to each step whether it is an arithmetic (meaning built-in operations on any class of data, and struct selectors), plugging (user-defined function application) or conditional (as in
cond
) step:-
(define (ev-quad a b c x) (+ (* a (sqr x)) (* b x) c)) (ev-quad 2 -3 4 2)
-
(define-struct quad (a b c)) (define (ev-quad2 q x) (ev-quad (quad-a q) (quad-b q) (quad-c q) x)) (ev-quad2 (make-quad 0 4 -20) 5)
-
(define (signum x) (cond [(positive? x) 1] [(negative? x) -1] [else 0])) (define (my-abs x) (* x (signum x))) (my-abs -8)
As a confidence check, you may run each program and make sure your stepping result agrees with DrRacket’s result. Be sure to write the steps in the same style we used in class, and don’t forget to label each arrow with one of “arith,” “plug,” or “cond”.
-
-
The CEO of the Chicago Mercantile Exchange has hired you to develop a data representation for commodity futures quotes. Each quote includes the name of some commodity (like “Corn” or “Swiss Franc”), the expiration date for the contract (like May 2019 or August 2020), the current price (like 2.535 or 121.6), and today’s change in price (like +0.2 or -1.075).
Design a data definition for representing a quote, including an interpretation. Define the constant
T-NOTE-QUOTE
as an example of a quote representing this information: 2-Year T-Note futures expiring in June 2019 are selling at 106.092, which is up 0.001 for the day.Design a function
most-improved
that takes two quotes and returns the quote with the larger relative change in price for the day, or#false
in case of a tie. (The relative change is the absolute change divided by the base amount.)(Hint: Think carefully about the signature of the function, because the data definition you just gave for quotes isn’t quite right for the function’s result.)
A library needs to keep track of their inventory. The library carries two kinds of media, books and videos, each of which is described by different information:
- each book has a title, an author, and a year of publication
- each video has a title, a director, a year of release, and a length in minutes
Design a data definition named
LibItem
for representing the information about an item of either kind; include an interpretation. Design a functionitem-title
that, given a library item, returns its title.-
The library from Problem 3 keeps item records in a computer (using DrRacket), but they also maintain a card catalogue. Here are sample card catalogue entries for a book and a movie:
Fred Brooks: "The Mythical Man-Month" (1975). "The Mythical Man-Month Movie" (dir. Ron Howard), 95 minutes (2021).
Design a function
format-card
that, given a library item record, returns the text of the card catalog entry.(Hint: To make a string with the double quotation mark
"
in it, writing"""
won’t work. You need to escape the double quote with a backslash, like"\""
. The backslash tells BSL that the next double quote is part of the string rather than the end of it.) -
The aforementioned library also needs to keep track of which books and films are checked out. In particular, they need lending records that associate an item (represented as in Problem 3) with the name of the patron who checked it out and the number of days until it’s due. (Use negative numbers for overdue items.)
- Design a data definition named
LibLoan
and provide an interpretation. Update: This has been updated, but the old names will also be accepted.old name new name CheckOut
LibLoan
item-overdue?
loan-overdue?
- Design a function
checkout-item
that, given a library item and the name of the person checking it out, returns theLibLoan
for that item. (Books are checked out for 14 days and videos for 7 days.) - Design a function
next-day
for the library to use at midnight daily to decrement each loans’s days remaining. In particular, it should take aLibLoan
and return a copy but with one fewer day remaining. - Design a predicate function
loan-overdue?
that determines whether the givenLibLoan
is overdue.
- Design a data definition named
Turn In
Please write all your code in one .rkt
file and
submit it through
Canvas.