Exercises

Exercise 5.1.   [*] Develop the function allergic. The function consumes a Request (see exercise 1.3 and produces the list of strings that are associated with false in the Request.

Compare allergic with toppings (also see exercise 1.3). Abstract the two functions into a single function.

Optional: Can you use the new function to extract all positive numbers from a list of numbers? If not, can you create a common abstraction? 

Exercise 5.2.   [challenging!] Compare these two functions:

;;  N  →  Image
;; add n squares to a 
;; 100 x 100 empty canvas
(define (add-boxes n)
  (cond
    [(zero? n) (empty-scene 100 100)]
    [else
      (place-image
	(rectangle 1 1 'green)
	(random 100)
	(random 100)
	(add-boxes (sub1 n)))]))

;; tests: 
(image=? (add-boxes 0) 
         (empty-scene 100 100))
(image? (add-boxes 10))
;;  N  →  Number
;; to add n to pi without using +

(define (add-to-pi n)
  (cond
    [(zero? n) pi]
    [else
      (add1 
	(add-to-pi (sub1 n)))]))




;; tests: 
(= (add-to-pi 0) pi)
(= (add-to-pi 3) (+ pi 3))
(= (add-to-pi 10) (+ pi 10))
Abstract the two functions into one. 

Exercise 5.3.   Develop the function blink. It consumes a natural number n and a position and draws and clears a blinking rectangle at that position n times.

Add the teachpack
draw.ss

Hint 1: Use sleep-for-a-while.

Hint 2: Develop two auxiliary functions: one for drawing an object and one for clearing it.

Design a common abstraction for the two auxiliary functions. Then change the function so that it draws/clears a “lunar lander.” Approximate the lander with one small rectangle atop a large rectangle and two short, rectangular legs sticking out from the large rectangle. 

Exercise 5.4.   Design all-numbers. The function determines whether all items in a list (of arbitrary values) are numbers.

Develop the function all-symbols, which determines whether all items in a list are symbols.

Abstract the two functions into a single function. Use the new function to define a function that determines whether all numbers on a list of numbers are positive. 

Exercise 5.5.   [*] Compare the following two data definitions:

;; A NumberBT is one of: 
;; --- (make-leaf Number)
;; --- (make-node NumberBT NumberBT)
;; A StringBT is one of: 
;; --- (make-leaf String)
;; --- (make-node StringBT NumberBT)
Abstract them into a single data definition for binary trees. 

Exercise 5.6.   Use every to define numbers-to-strings from exercise 1.2