Honors-section homework
You have to work on this problem set with the same partner you had for hw4h.
You must follow the design recipe and the guidelines when you develop functions.
Lists and More Lists!
Problem A1
The local meteorological society keeps a list of records about the
weather each day. They track the following attributes: zip code, humidity (as a
percentage), and high and low temperatures (in Fahrenheit) for the day.
Here is the data definition for a weather record:
(define-struct weather (zip humidity high low))
;; A Weather is a (make-weather String Number Number Number)
;; Intepretation:
;; The weather's zip is the 5-digit zip code where the data was collected.
;; The weather's humidity is a percentage.
;; The weather's high and low represent the day's high and low temperatures in Fahrenheit.
-
Design a data definition for a list of weather records
(
LoWR
). Note that a list of weather records may contain
multiple records for the same zip code.
-
Design a function
muggy
to eliminate weather records
with a humidity lower than humid-threshold
or a high
temperature lower than temp-threshold
. The function
consumes a list of weather records, called lowr
, a number
humid-threshold
(expressed as a percentage), and a number
temp-threshold
(that denotes a temperature in degrees
Fahrenheit), and produces a list of only those weather records in
lowr
whose humidity and high temperature are greater than
or equal to humid-threshold
and
temp-threshold
, respectively.
-
The meteorological company has just been informed of a problem with
temperature readings at all locations in zip code 02138. The high and
low temperatures on file for this zip code are 4 degrees higher than
the actual high and low temperatures of the day.
Design a function adjust-temps
that takes a list of
weather records called lowr
, a string called
zip
representing the zip code, and a number called
adjustment
, and produces a list of weather records that
contains all the records in
lowr
but with the high
and low
temperatures in any record with zip code zip
replaced by
high + adjustment
and low + adjustment
.
Using your function, the meteorological society can fix its list of
weather records for October 3rd, called lowr-oct-3-2012
,
by running (adjust-temps lowr-oct-3-2012 "02138" -4)
.
Problem A2
The half-open interval [5,10) contains the following integers:
5,6,7,8,9. More generally, if start
and end
are integers and start
is less than end
, then
[start,end
) denotes the half-open interval that
includes all the integers from (and including) start
to
(and excluding) end
.
Develop the function evens-in-interval
which takes two
integers start
and end
. The computation it
performs depends on the inputs:
-
If
start >= end
, the function's output is
'invalid-interval
(indicating that
[start,end
) is not a valid half-open interval).
-
If
start < end
but there are no even integers in
half-open interval [start,end
), the
function's output is 'none
(indicating that there
are no even integers in this half-open interval).
-
If
start < end
and there is at least one even integer in
the half-open interval [start,end
), the function's output
is the list of even integers in ascending order. For example,
(evens-in-interval 5 10)
produces the output
(cons 6 (cons 8 empty))
.
Hint: Do some wishful thinking: try to figure out what
helper functions you should design for this problem.
Graphical Editor, Revisited
Re-develop your graphical editor from the last assignment using a
slightly different data representation.
A 1String is a string, s, whose length is exactly
one, i.e. (= (string-length s)
1)
. Note: 1String is not a valid BSL
identifier, but it works fine as a name for your data representation.
A list of 1Strings (Lo1String) is one of:
empty
(cons 1String Lo1String)
Revise the Editor data defintion from the first part of the
last assignment to only use 1Strings and Lo1Strings:
(define-struct editor (pre post))
;; An Editor is a (make-editor Lo1String Lo1String)
Re-develop the editor program using this new definition (Exercise
1-5).
Exercise 6: Discuss the design trade-off in a paragraph of 20
to 30 words. (We will deduct points for each word in excess of 30.)
Pocket Calculator
In this part of the assignment you will develop the basic pieces of a
small pocket calculator for arithmetic expressions.
An arithmetic expression can either be a number, a variable, a unary
operation applied to one argument, or a binary operation applied to
two arguments. So for example, 3 + 4 is an arithmetic
expression -- the binary operation + is applied to the
arithmetic expressions 3 and 4. Arithmetic
expressions nest of course, and we use parenthesis to indicate the
nesting, so for example, (1 / 2) * (x + (3 + 4)) is an
arithmetic expression. The basic operations are (unary) -,
which flips the sign of its argument, and (binary) -, +, *,
/, which perform subtraction, addition, multiplication, and
division, respectively.
Exercise 7: Develop a data definition for arithmetic
expressions without variables.
Exercise 8: An expression that contains no variables can
be evaluated. For example, 3 + 4 evaluates
to 7. Develop a program eval-arith
that
evaluates a given arithmetic expression (without variables).
Exercise 9: Develop a data definition for arithmetic
expressions that may contain variables. Hint: what can you
reuse?
Exercise 10: Develop a program plug-in
that
consumes an arithmetic expression (that may contain variables), a
variable, and a number, and produces a similar arithmetic expression
where all occurrences of the given variable are replaced with the
given number. So for example, plugging 5 in for x
in (the representation of) 2 * (x * x) should produce the
expression (that represents) 2 * (5 * 5).