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).
- In class we learned about the three things that every programming language has. What are they? Give an example of each from Racket to illustrate your answer.
- If a programming language supports higher-order functions, it does not have to support functions of more than one argument. Explain why.
-
We say that
ifis a special form. What does that mean? List one way we can know that is true fromif's behavior. -
What does it mean to say that that
+is a variable-arity 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:
(cons (list 'a 'b) (cons (cons 'c 'd') '()))
-
Write the Racket literal corresponding to the list shown
in this box-and-pointer diagram.
Problem 3
The Leyland number of x and y equals
xy + yx.
Write a Racket function named leyland that
takes two arguments, both numbers, and returns their
Leyland number. For example:
> (leyland 1 2) ; 12 + 21 3 > (leyland 2 4) ; 24 + 42 32 > (leyland 8 42) ; 842 + 428 85070591730234615865843661540594049280
Remember: Racket has a built-in function named
expt that computes a number raised a power.
Problem 4
Each year, a company has to compute the income tax for every one of its employees. The tax is a percentage of the income above a base amount that is not taxed. The percentage and tax-free amount change from year to year.
Write a Racket function named tax-at that takes
two arguments, both numbers: a tax-rate and
base.
tax-at returns a function as its value. That
function takes one argument, a number income,
and returns the tax on that income. For example:
> ( (tax-at 0.15 100) 1100 ) ; 0.15 * (1100 - 100) 150.0 > ( (tax-at 0.15 100) 500 ) ; 0.15 * (500 - 100) 60.0 > (define this-years-tax (tax-at 0.05 1000)) > (map this-years-tax '(1500 2000 3000 10000)) '(25.0 50.0 100.0 450.0)
You may assume that the income is ≥ the base. Extra credit for properly handling incomes less than the base.
Problem 5
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. For example, this is the entry for October 27, 2024:
("ALO" "2024-10-27" 65.0 36.0 0.0 10.25 0.0)
Write a Racket function named temp-variation
that takes two arguments, an integer baseline
and a list of daily weather data in this form.
temp-variation computes the total of the
differences between baseline and each day's high
temperature. For example:
> (define two-days
'( ("ALO" "2024-01-01" 27.0 22.0 0.0 6.430424 0.0)
("ALO" "2024-10-27" 65.0 36.0 0.0 10.25392 0.0) ))
> (temp-variation 58 two-days) ; abs(27-58) + abs(65-58)
38
; computes the total variation for all of 2024
> (temp-variation 58 weather-data)
6966
Remember: Racket has a built-in function named
abs that computes the absolute value of a number.
Use the higher-order functions apply and
map as needed to implement your function.
No recursion, looping, or sorting are allowed.