If you run any of the functions (as opposed to just testing them), comment out the code for running the functions before you submit your solution.
N.B.: Tests are not runs. A test is when you evaluate an expression and compare it to some expected result, while a run is when you evaluate an expression without anticipating any particular result for the expression.
Due date: 11/10 @ NOON
This problem set covers abstraction and "loops" again.
HtDP Problems:
21.2.1, 21.2.2(2), 21.2.3(2)
Problem 1:
Parameteric Sexpressions are like those you know as Web pages but they may
contain all kinds of things:
;; An [Sexp X] is one of:
;;  empty
;;  (cons [Sexp X] [Sexp X])
;;  (cons X [Sexp X])
Make up one example per [Sexp Number], [Sexp Symbol], and
[Sexp (union Number Symbol)].
Here are two functions that traverse Sexpressions and process the leafs (X):
;; count : X [Sexp X] > Number
;; count all occurrences
;; of x in s
(define (count x s)
(cond
[(empty? s) 0]
[(cons? (first s))
(+ (count x (first s))
(count x (rest s)))]
[else
(cond
[(equal? (first s) x)
(+ (count x (rest s)) 1)]
[else
(count x (rest s))])]))

;; rpl : Y X [Sexp X] > [Sexp Y]
;; replace all occurrences
;; of old in s with new
(define (rpl new old s)
(cond
[(empty? s) empty]
[(cons? (first s))
(cons (rpl new old (first s))
(rpl new old (rest s)))]
[else
(cond
[(equal? (first s) old)
(cons new (rpl new old (rest s)))]
[else
(cons (first s)
(rpl new old (rest s)))])]))

Design the common abstraction traverse and a general contract.
Design the function
;; sum : [Sexp Number] > Number
;; compute the sum of all numbers
;; that occur in the given Sexpression
For full credit, define the final version in terms of
traverse. Remember that design is not a onestop activity.
Design the function
;; rem : Symbol [Sexp Symbol] > [Sexp Symbol]
;; remove all occurrences of x from s
(define (rem x s) ...)
For full credit, define the final version in terms of traverse.
Design the function
;; flatten : [Sexp X] > [Listof X]
;; collect all X's in a list
For full credit, define the final version in terms of traverse.
Determine which of these three functions are parameteric and which are
not. Explain why.
Problem 2:
Define the following function using a loop:
;; numberof : Listofdigits > Number
;; convert a list of digits [0..9] into a number
;; the leastsignificant digit is first
(define (numberof lod)
(cond
[(empty? lod) 0]
[else (+ (first lod) (* 10 (numberof (rest lod))))]))
;; Tests:
(= (numberof (list 9)) 9)
(= (numberof (list 1 9)) 91)
Define the following function using a loop:
;; juxtapose : Image Image > Image
;; create a single image by aligning
;; the right side of i with the left of j
(define (juxtapose i j)
(overlay/xy i (imagewidth i) 0 j))
;; juxtapose : (cons Image (Listof Image)) > Image
;; juxtapose a nonempty list of images
(define (juxtapose* loi)
(cond
[(empty? (rest loi)) (first loi)]
[else (juxtapose (first loi)
(juxtapose* (rest loi)))]))
;; Tests:
(define image1 (rectangle 10 10 'solid 'green))
(define image2 (rectangle 10 10 'solid 'blue))
(define image3 (rectangle 10 10 'solid 'red))
(define image1+2
(overlay/xy (rectangle 20 10 'solid 'green)
5 0
(rectangle 10 10 'solid 'blue)))
(define image123
(overlay/xy image1+2 15 0 (rectangle 10 10 'solid 'red)))
(equal? (juxtapose image1 image2)
image1+2)
(equal? (juxtapose* (list image1 image2 image3))
image123)
Hint: Experiment and see for yourself. Then guess.
Define bullseye using loops:
;; bullseye : NaturalNumber > Image
;; draw a black bull's eye with i rings with distance 10
(define (bullseye i)
(draw (indices i 10)))
;; indices : NaturalNumber Number > (Listof Number)
;; create i * delta, ..., delta
(define (indices i delta)
(cond
[(zero? i) empty]
[else (cons (* i delta) (indices (sub1 i) delta))]))
;; draw : (cons Number (Listof Number)) > Image
;; create bull's eye from concentric radii
(define (draw l)
(cond
[(empty? (rest l)) (circle (first l) 'outline 'black)]
[else (overlay (circle (first l) 'outline 'black)
(draw (rest l)))]))
Start by developing exercises and turning them into tests. Hint: Here is a
small experiment:
> (indices 3 10)
(list 30 20 10)
> (buildlist 3 (lambda (i) (* 10 (+ i 1))))
(list 10 20 30)
Use the insight. You may wish to proceed in a stepwise fashion and reorganize
the program as you go.