A4 — Recap of names, functions, and tables
Review names, functions, evaluation,and tables.
Due Sep 23, 2026
Estimated time to complete (just the assignment, excluding the prep materials): 1.5–3 hours.
Prep materials: Weeks 1, 2, 3
What to do
For each part of the assignment, create a file with the specified name. Make sure the very first line use context... is correct in each file.
Submit all your files to Assignment 4 Submission on Brightspace. Make sure all files run.
Expressions (file a4-expr.arr)
Write a Pyret expression that produces a solid yellow circle with the radius 12. Do not define a name for it, just write the expression in the program file. When you press Run, you should see the yellow circle.
Write a Pyret expression that computes \(\sqrt{3^2 + 4^2}\). Do not define a name for it, just write the expression in the program file. When you press Run, you should see 5 after the yellow circle.
Define
exp-4nto be the expression that computes \(\sqrt{0^2 + (-4)^2}\). When you press Run, you should still see the yellow circle followed by 5. Do you understand why? If not, find out.Add a line that contains just
exp-4nto your program file. When you press Run, you should see the yellow circle, followed by 5, followed by 4. Do you understand why? If not, find out.Define
exp-42to be any expression that evaluates to 42 and that is not itself a value. If you don’t know what “expression” and “value” mean, review the prep materials. How can you check that your definition indeed evaluates to 42?Define a function
hypotenusethat takes two numbers,aandb, as parameters and returns the length of the hypotenuse, computed as the square root of the sum of the squares ofaandb.Define a name
hypotenuse-13to be the result of calling yourhypotenusefunction with the arguments-5and12.Ungraded, submit your response to the journal.
What is the difference between13,hypotenuse-13, andhypotenuse(-5, 12)? What does Pyret need to do when it evaluates each of these three expressions?Click here for a hint
Try this program.
Functions and conditionals (file a4-fun.arr)
Write a function
absolutethat takes a number and returns its absolute value.Read the intro part of Pyret’s documentation on numbers (it’s okay if you cannot understand everything it says). Find out how to check if a number is a rough number (
Roughnum). Definepi-roughto be the result of checking if \(\pi\) is a rough number.Write a function
is-oddthat takes a number and returnstrueif it is odd andfalseotherwise.
Hint: in the numbers documentation, search for “remainder”.Modify the function
is-oddso that it raises an exception"is-odd: argument must be an integer"if its argument is not an integer.
Hint: reference the numbers documentation again.Write a function
weatherthat takes a non-negative numbertemp-fas a parameter (temperature in Fahrenheit) and returns a string that describes the weather based on the following rules in Celsius:- If the temperature is less than or equal to 0, return
"freezing". - If the temperature is between 0 (exclusive) and 10 (exclusive), return
"cold". - If the temperature is between 10 (inclusive) and 20 (exclusive), return
"cool". - If the temperature is between 20 (inclusive) and 30 (exclusive), return
"warm". - If the temperature is 30 or above, return
"hot", except for the case of exactly 38, when the function should return"magic fever threshold".
- If the temperature is less than or equal to 0, return
Recover your
calcfunction from Assignment 2 (or grab it from the sample solution posted on Discord). Modifycalcso that it supports a new operation,%, which computes the remainder of the first argument divided by the second argument. If at least one of the arguments is not an integer, or if the second argument is zero, the function should raise an exception"Error: invalid argument(s) of %".Optional, ungraded, submit your response to the journal.
Considercalcfrom Assignment 2: you can recover your own definition or use the one published on Discord; either way, make sure you have argument and return type annotations.How can we modify
calcsuch that when it’s called ascalc("true", "+", "false"), the function returns"true"? More generally, how do we make it return the logical or of the first and last argument when both of those arguments are either"true"or"false"?Try to modify
calcand see if it works. If not, why? Relying only on the knowledge you have gained so far in this course, how could you make this function work?Click here for a hint
You are allowed to alter type annotations.
Tables (file a4-tbl.arr)
Define a function
fthat takes a numberxand computes \(-2x + 3\).Define a table
tbl-xthat has one columnxwith values -3.5, 0, 5, -1, 2, 0.25, 2.Define a table
tbl-fthat is similar totbl-xbut also has a columnfthat contains the values off(x)for each value ofxintbl-x. Do not compute the new column manually.Define
tbl-f-sortedto be the same astbl-fbut sorted by the values in thexcolumn. Do not create it manually.Define
f-minto be the smallest value off(x)intbl-f-sorted, without computing it manually.Optional (and more difficult), ungraded, submit your response to the journal.
For the basic version ofcalcthat computes only+,-,*,/, and^, could you replace the multi-branch if-expression with a use of a table?Click here for hints
- Consider
filter-withand how it uses a function as an argument. - Experiment with the expression
_ + _, e.g.(_ + _)(2, 3). What do you think it means? - Search the Pyret documentation on tables for table length.
- Consider
Grading
The code will be auto-graded for correctness, but remember that coding style and program design will be graded on exams.