Homework 3:
Higher-Order Functions in Racket
Due: Monday, February 5, at 11:59 PM
Introduction
This assignment gives you your first chance to write and use higher-order functions in Racket.
Template Source File
Download
this template file
and use it as the starting point for your submission. Please name
your file homework03.rkt
!
homework03.rkt
includes a require
expression at the top. It imports the rackunit
module and enables you to write test cases for your solutions.
The template file contains several test cases for each problem.
Do Not Use...
To solve these problems, you do not need any Racket features beyond the things we have learned in class and the things discussed in this assignment.
-
Do not use a
let
expression in any function. -
Do not use an internal
define
in any function. -
Do not use explicit recursion or looping in any
function. Your solutions to Problems 3, 4, and 5 should use
higher-order functions such as
apply
andmap
to do their jobs.
Helpful Functions
You may find these Racket primitives useful on this assignment:
-
string->number
converts a string that contains a number into the equivalent number. -
min
takes any number of numeric arguments and returns the smallest. -
exact->inexact
converts an exact number (an integer or a fraction) into the equivalent floating-point number. -
You may pass a two-argument function to
map
. If you do, pass two list arguments, not one.map
will pass the corresponding items in each list to the function at the same time:> (map + '(1 3) '(2 7)) '(3 10)
You do not have to use this, but you may if you think it helps
Problems
-
For Problem 2 of Homework 2, you
wrote a function named
price-per-ounce
that computes the price per ounce for a pack of soda. In practice, there are a number of difference combinations in which packs come, such as 12-packs of 12-ounce cans, 6-packs of 24-ounce bottles, and 12-packs of 8-ounce cans. It would be helpful to have specialized price-per-ounce functions for each of these standard packs.
Write a Racket function namedprice-per-ounce-for
that takes two arguments: the number of units (bottles or cans) in the pack and the size of each unit in ounces.price-per-ounce-for
returns a function that takes one argument, the price for a pack. The returned function returns the price of the pack per ounce. For example:> ((price-per-ounce-for 6 16.9) 1.44) 0.01420118 > (define big-bottles-at (price-per-ounce-for 6 24)) > (big-bottles-at 1.44) 0.01
I have providedcheck-=
expressions for these two examples in your template file. Write at least two morecheck-=
expressions to test your solution. -
For Problem 3 of Homework 2, you wrote
a function named
ring-area
that computes the area of the ring formed by two concentric circles. In manufacturing, we often work in a setting with one of the radii fixed. For instance, we may need to make washers of various sizes that have a fixed-size hole in the middle. In this case, the inner radiusri
is fixed.
Write a Racket function namedring-area-around
that takes one number as an argument: the inner radiusri
, in inches.ring-area-around
returns a function that takes one argument, the the outer radiusro
, also in inches. The returned function returns the area of the ring formed by the concentric circles. For example:> ((ring-area-around 1) 2) 9.42477796076938 > (define area-for-2inch-bolt (ring-area-around 2)) > (area-for-2inch-bolt 5) 65.97344572538566
I have providedcheck-=
expressions for these examples. Write at least two morecheck-=
expressions to test your solution. -
Suppose that we have a list containing height/weight pairs
for a group of people, in inches and pounds, respectively:
( (76 . 195) (81 . 212) (79 . 225) (78 . 206) ... )
We would like to know the average weight of the people in the group.
Use higher-order functions such asmap
andapply
to define a Racket function namedaverage-weight
. This function takes a list of height/weight pairs as its only argument and returns the average weight of the group. For example:> (average-weight '((79 . 225))) 225.0 > (average-weight '((70 . 150) (62 . 100))) 125.0
Assume that we have already written a function namedaverage
that takes any number of numeric arguments and returns their average. (It is given in the template code file.) You may write other helper functions if you like, but you do not have to.
I have providedcheck-=
expressions for these examples. Write at least two morecheck-=
expressions to test your solution. -
Suppose that we have a list of scores from the UNI women's
basketball team this season. Each score is a list of
size two, whose first value is the number of points UNI scored
and whose second value is the number of points UNI's opponent
scored. For example, after the third game of the season this
year, we had:
((102 51) (78 67) (53 94))
This list contains three games. UNI won the first 102-51, won the second 78-67, and lost the third 94-53.
The margin of victory in a game is the (positive) difference between the two teams' scores. For Game 1, the margin of victory wasabs(102 - 51) == abs(51) == 51
points. For Game 3, the margin of victory wasabs(53 - 94) == abs(-41) == 41
points. The list can contain any number of these pairs.
Write a Racket function namedtotal-margin
that takes one argument, a list of this form. The function returns the total of all the margins of victory in the list. For example:> (total-margin '((102 51) (78 67) (53 94))) ;; 51 + 11 + 41 103 > (define uni-women ;; all 18 games thus far '((102 51) (78 67) (53 94) (64 75) (54 71) (64 68) (65 84) (62 115) (59 78) (70 87) (52 54) (82 52) (85 79) (76 52) (95 75) (67 72) (77 88) (105 59))) > (total-margin uni-women) 387
I have providedcheck-=
expressions for these two examples in your template file. Write at least three morecheck-equal?
expressions to test your solution. -
To monitor enrollments each semester, I have a spreadsheet that
contains a list of courses with names, enrollments, and
capacities. I read the spreadsheet data into a Racket list that
looks like this:
'(("Dept" "Number" "Section" "Class Nbr" "Capacity" "Enrolled") ("CS" "1000" "1" "11546" "30" "30") ("CS" "1025" "1" "11547" "30" "30") ("CS" "1120" "1" "11557" "30" "15") ("CS" "1130" "1" "11548" "30" "18") ... )
The first item in the list is the header row in the spreadsheet. It is not part of the data.
The dean and provost frequently ask me for various summary data, such as total enrollments or remaining capacity.
Write a Racket function namedmin-open-seats
that takes such as a list as its only argument. It returns the minimum number of open seats available in any section. For example:> (define example '(...)) ; the data shown above > (min-open-seats example) 0
CS 1025 has 30 - 30 = 0 open seats. The other classes have 0, 15, and 12 open seats respectively.
I have provided acheck-equal?
expression for this example. Write at least two morecheck-equal?
expressions to test your solution.
Deliverables
Use Save Definitions to save the file of function
definitions you create using the template you downloaded.
It will have the file extension rkt
. Be sure to use
the specified name for your file! This enables the autograder to
find and run your code.
By the due time and date, use the course submission system to submit the following files electronically:
-
homework03.rkt
, the source file containing your function definitions and test cases
No hard copy is required.
Be sure that your submission follows the submission requirements. Be sure to use the specified name for your file. This enables the autograder to find and run your code.