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!

  1. (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.

  2. (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 the coin function works as intended.

(defun coin ()
  (cond ((< (random 101) 50) 'heads)
        ((> (random 101) 50) 'tails)
        (t 'edge)))
  1. Write (beforep x y input) which returns true if x appears before y in the input list.

  2. (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.

  3. (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 use mapcar.

  4. (SC, 7.8) Write a function that takes two inputs, xs and k, and returns the first number in the list xs that is roughly equal to k. Let’s say that “roughly equal” means no less than k − 10 and no more than k + 10. Use loop.

  5. Rewrite your answer to the previous function using find-if.

  6. Get the paper handout and do (SC, 7.10).