Due: May.31 (Tue) 17:30, on D2L.
Your submitted file should be named “hw02b.rkt” as plain-text (which is what DrRacket saves as).
Standard instructions for all homeworks:
Your name,
the assignment-number,
and
the URL of this assignment
must be in a comment at the start of the file.
Any code which results in syntax errors
will generally not earn more than 50%
of the points-possible.
For grading, I will ignore any commented-out code.
Format well.
(In particular,
each open-paren should be preceded by a space or start-of-line, for now.
E.g.(+ 2 (* 3 4 5)), not ( + 2(* 3 4 5) ).)
It's intended that you complete hw02 (on D2L) before working on
this problem.
Follow the design recipe,
steps 4,5,7,8.
(You don't need to label each step; just turn in the final result.)
(10pts)
Recall that in May.17's lecture, we demo'd the steps of writing a
simple function,
pizza-area,
which computed the area of a pizza given diameter.
We will write a couple of similar functions.
Of course,
Pizzas have topping in the middle, and crust around the edge.
(You can think of the full pizza being a disc,
with a smaller pure-topping-covered disc inside of it.)
Define a named-constant CRUST-WIDTH, which should be 2 inches.
Some people ignore the crust of a pizza, and just want to know
how much topping-covered area they'll be getting when they order a pizza.
Write topping-area,
which returns the amount of pizza covered in topping (in square-inches),
for a given diameter (in inches).
Other people love the crust, and only care about how much crust is on their pizza!
Write crust-area, which returns the amount of crust (in square-inches)
for a pizza of a given diameter (in inches).
(10pts)
Write the function has-suffix?,
which takes in two strings and returns whether or not the first string
ends with (the letters of) the second.
Note that this is a computer-sciency definition of “suffix”.
It has nothing to do with syllables or English.
More precisely:
we say that string a has b as a suffix, iff:
there exists a string x such that
(string=? a (string-append x b))1.
hint:You can certainly use cond (or if)
if you like, but they're not actually needed; you can instead use a
min or max
in a clever (but standard) way.
conditions/requirements
In addition to the general requirements above,
here are some more specific to this homework:
For both functions, be sure to follow steps 4,5,7 of the design recipe.
(That is, when grading, there are points for having a good purpose-statement,
good tests, etc. In general, test cases alone can be worth nearly half the points.)
Your programs should be racket's beginning-student language.
For now, your code does not need to give a correct
answer for pizzas whose diameter is less
than twice the CRUST-WIDTH.
However, you should still have tests for that situation,
which contain the true, expected
answer in the check-expect.
(So, it's fine if those tests don't pass.
If you really want to get your code working for that case -- again, NOT required --
you can either read about `cond`, or be clever in using the functions `max` or `min`.)
Requirement: Don't repeat code!
If you find yourself repeating similar calculations
to what's already in pizza-area, see if
you can avoid the repeated calculations
by calling pizza-area as a helper-function
(perhaps a couple of times).
1
We'll see later how, in a declarative language (Prolog),
this definition alone constitutes runnable code.
↩