Quiz 1
Instructions
- The quiz consists of five questions and one image.
- Read all the questions thoroughly before you begin. It is worth your time to plan ahead!
- The quiz is worth 60 points. Each question is worth twelve points.
- Partial credit will be given where possible, so show your work.
- The quiz lasts thirty (30) minutes. It is due at 1:45 PM.
Problem 1
Write brief answers for each of the following items (two sentences each, max).
- One of the three things that every programming language has is a means of combination. What is Racket's only means of combination? Give an example to illustrate your answer.
- What are the other two things that every programming language has? Give an example of each from Racket.
-
We say that
define
is a special form. What does that mean? List one way we can know that is true fromdefine
's behavior. -
What does it mean to say that that
map
is a higher-order function?
Problem 2
Answer each of these questions about the structure of Racket lists.
-
Draw a box-and-pointer diagram that shows the structure
of this Racket list:
(list 3 (cons 1 6) (cons 2 (cons 4 '())))
-
Write the Racket literal corresponding to the list shown
in this box-and-pointer diagram.
Problem 3
When we go to the store, we pay a small sales tax on each item. we buy. The total price for any particular item is (the price of the item) × (the number of items bought) × (1 + the tax rate).
Write a Racket function named line-item-cost
that
takes three arguments, all numbers: the item price, the number
of items purchased, and the tax rate.
line-item-cost
returns the total cost for this
purchase, including sales tax. For example:
> (line-item-cost 1.00 1 0.05) ; $1.00 x 1, plus 5% 1.05 > (line-item-cost 5.00 4 0.07) ; $5.00 x 4, plus 7% 21.40 > (line-item-cost 10.00 6 0.15) ; $10.00 x 6, plus 15% 69.00
Problem 4
In Session 7,
we worked with lists of daily weather data. Each day's weather entry
looked like this:
(city date high-temp low-temp precipitation wind-speed snow)
.
The first two items are strings, and the rest are numbers.
Write a Racket function named amount-over
that
takes one argument, a number baseline
.
amount-over
returns a function as its value. That
function takes one argument, a day's weather entry, and returns
how much the day's high temperature is above
baseline
. For example:
> (define jan01 '("ALO" "2024-01-01" 27.0 22.0 0.0 6.430424 0.0)) > (define oct27 '("ALO" "2024-10-27" 65.0 36.0 0.0 10.25392 0.0)) > ( (amount-over 50) oct27 ) 15.0 > ( (amount-over 0) jan01 ) 27.0 > (define amount-above-average (amount-over 58)) > (map amount-above-average (list jan01 oct27)) '(-31.0 7.0)
Recall that Racket has list accessors first
through
tenth
.
Problem 5
Suppose that we have a list of pixels from an image, where each
pixel is a list (r g b)
containing the red, green,
and blue (RGB) values of the pixel. (76 195 202)
is an example of a pixel. For example, an image might look like
this:
( (76 195 202) (255 71 208) (175 81 214) (0 165 81) ... )
Write a Racket function named least-red-green
that
takes one argument, a list of pixels lst
in this
form.
least-red-green
computes the sum of the red and
green values for every pixel in lst
and returns
the smallest of the sums. For example:
> (least-red-green '( (1 3 5) )) 4 > (least-red-green '( (0 0 240) (100 120 140) )) 0 ; trust me: this pixel has smallest red + green ; vvvvvv > (least-red-green '( (76 195 202) (255 71 208) (175 81 214) )) 256
Remember: Racket has a variable-arity function named
min
.
Use the higher-order functions apply
and
map
as needed to implement your function.
No recursion, looping, or sorting are allowed.