Homework Assignment 2
Basic Functions in Racket
CS 3540
Programming Languages and Paradigms
Spring Semester 2023
Due: Tuesday, January 31, at 10:00 AM
Introduction
- Template Source File
This assignment gives you your first chance to write Racket
procedures. Download the file
homework02.rkt
and use it as a template for your submission. Please use the
name homework02.rkt for your file!
This file includes a require expression at the
top. It imports the rackunit module and enables
you to write test cases for your solutions [
example code
]. The template file contains several test cases for each
problem.
- Do Not Use...
You do not need any advanced Racket features to solve these
problems, only simple expressions.
- Do not use a let expression in any
function.
- Do not use an internal define in
any function.
- Helpful Functions
You will find these Racket primitives useful on this assignment:
You may want to use each for at least one problem.
To cause a number to evaluate as a floating-point number, you
can make one of the arguments floating-point. In Problem 1,
dividing by 180.0 instead of 180 will cause Racket to return
a floating-point number.
Problems
- Write a Racket function named quiz-percentage
that takes exactly three integer arguments. Each argument will
be in the range 0 .. 60. quiz-percentage returns
the percentage of the points earned. For example:
> (quiz-percentage 40 20 30) ; average is 50% -- 90/180
0.5
> (quiz-percentage 60 50 40) ; average is 150/180, or 0.83...
0.8333333333333334
> (quiz-percentage 60 60 60) ; perfect score: 100%!
1.0
I have provided check-= expressions for these three
examples in your template file.
- On Homework 1, you used the
quadratic formula
to find the roots of a quadratic equation. The expression under
the radical, b2 - 4ac, is called the
discriminant. It can be used to determine how many
solutions (0, 1, or 2) the equation has.
Write a Racket function named disc that takes three
arguments, all integers: a, b, and c.
disc returns b2 - 4ac. For example:
> (disc 1 4 4) ; x2 + 4x + 4
0 ; ... has one real solution
> (disc -3 8 -4) ; -3x2 + 8x - 4
16 ; ... has two real solutions
> (disc 2 1 3) ; 2x2 + x + 3
-23 ; ... has zero real solutions
I have provided check-= expressions for these three
examples in your template file.
-
A 10-foot ladder leans against a wall. If its base is 6 feet
away from the bottom of the wall, then it reaches 8 feet high
on the wall. This is a simple example of the
Pythagorean theorem.
Write a Racket function named ladder-height
that takes two arguments, the length of the ladder and the
distance at the base. Both are in feet. The function returns
the distance up the wall reached by the ladder, also in feet.
For example:
> (ladder-height 10 6)
8
> (ladder-height 13 5)
12
> (ladder-height 20 3.5) ; that's steep... be careful!!
19.691368667515217
I have provided check-= expressions for these three
examples in your template file.
- According to
The Joy of Cooking,
when you are cooking candy syrups, you should cook them 1 degree
cooler than listed in the recipe for every 500 feet of elevation
you are above sea level. For example, the recipe for Chocolate
Carmels calls for a temperature of 244° Fahrenheit. If you
were making your Chocolate Carmels in Denver, the Mile-High City,
you would want to cook the syrup at 233.44°.
Write a Racket function named candy-temperature
that takes two arguments, the recipe's temperature in degrees
Fahrenheit and the elevation in feet, and returns the temperature
to use at that elevation. For example:
> (candy-temperature 244 5280) ;; Denver, baby!
233.44
> (candy-temperature 302 977.69) ;; the highest point in Cedar Falls
300.04462 ;; is approx. 298m above sea level
> (candy-temperature 302 -1401) ;; the Dead Sea 1401 ft below sea level
304.802
I have provided check-= expressions for these three
examples in your template file.
- Generally, the dimensions of engineered components are not exactly
the specified value, but rather within a certain tolerance of the
specified value. The tolerance generally depends upon the
application and the material being used. For example, a metal
piece used in construction that is listed as 5 cm in length might
actually be any length within 1 mm of 5 cm, that is, between 4.9 cm
and 5.1 cm, inclusive.
Write a Racket procedure named in-range? that takes
three numbers as arguments: two numbers to compare, and a tolerance,
epsilon. in-range? returns true if its first two
arguments are within epsilon of one another, and
false otherwise. For example:
> (in-range? 4.95 5.0 0.1)
#t
> (in-range? 4.95 5.0 0.01) ;; not anymore!
#f
> (in-range? 5.0 4.95 0.1) ;; works both ways
#t
> (in-range? 5.0 5.95 0.1)
#f
> (in-range? 5.5 5.95 0.5)
#t
I have provided check-true and check-false
expressions for all of these examples in your template file.
Deliverables
By the due time and date,
submit
the following files electronically:
- homework02.rkt, your file of function definitions,
and
- interactions.txt, an Interactions session that
demonstrates each of your functions on a new example or two.
No hard copy is required.
Be sure that your submission follows all of the
submission requirements.
Use Save Interactions As Text... to create the file of
interactions that you submit, and change the file extension to
txt.
Be sure to use the specified names for your files! This enables
an autograder to find and run your code.
Eugene Wallingford .....
wallingf@cs.uni.edu .....
January 24, 2023