Ch5 HW2
The purpose of this homework is for you to practice your Lisp program-writing skills. If you find yourself writing “Racket code”, ask for help before it is due!
-
(SC, 4.22) Write a predicate BOILINGP that takes two inputs, TEMP and SCALE, and returns T if the temperature is above the boiling point of water on the specified scale. If the scale is FAHRENHEIT, the boiling point is 212 degrees; if CELSIUS, the boiling point is 100 degrees. Also write versions using IF and AND/OR instead of COND.
-
(SC, Chapter 5) Write a program to return the distribution of results generated by N=100 calls to the function below. The distribution should be an association list of the form
'((heads 48) (tails 49) (edge 3))
. After you do that, explain if you believe thecoin
function works as intended.
(defun coin ()
(cond ((< (random 101) 50) 'heads)
((> (random 101) 50) 'tails)
(t 'edge)))
-
Write
(beforep x y input)
which returns true ifx
appears beforey
in theinput
list. -
(SC, 6.18) Write a function ADD-VOWELS that takes a set of letters as input and adds the vowels
(A E I O U)
to the set. For example, calling ADD- VOWELS on the set(X A E Z)
should produce the set(X A E Z I O U)
, except that the exact order of the elements in the result is unimportant. -
(SC, 7.7) Write a function
upside-down
that takes in a list such as'(up down up up)
and flips each element, returning'(down up down down)
. The only elements in the list will be up and down. Method to use: write an anonymous flipper function and usemapcar
. -
(SC, 7.8) Write a function that takes two inputs,
xs
andk
, and returns the first number in the listxs
that is roughly equal tok
. Let’s say that “roughly equal” means no less thank − 10
and no more thank + 10
. Useloop
. -
Rewrite your answer to the previous function using
find-if
. -
Get the paper handout and do (SC, 7.10).