Quiz 1

Instructions

Problem 1

Write brief answers for each of the following items (two sentences each, max).

Problem 2

Answer each of these questions about the structure of Racket lists.

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.