;; AnN(natural number) is either ;; ---0;; ---(add1 N)
Recognize 0 with zero?. Given n and assuming
(zero? n) is false, then n is (add1 m)
for some m, which belongs to N. We get m via
(sub1 n).
#| Template: ;; \scheme{natural-number -> ???} (define (process-n k) (cond [(zero? k) ...] [else ... (process-n (sub1 k)) ...])) |# ;;N Symbol → ListOfSymbols;; make a list withkcopies ofa;; examples: ;; --- given0and'z, produceempty;; --- given2and'z, produce(list 'z 'z)(define (copies n a) (cond [(zero? n) empty] [else (cons a (copies (sub1 n) a))])) ;; Tests: (equal? (copies 0 'z) empty) (equal? (copies 2 'z) (list 'z 'z)) ;;N → ListOfString;; create a list of strings"k", ...,"0"(define (cells k) (cond [(zero? k) (list "0")] [else (cons ( number→ string k) (cells (sub1 k)))])) ;; Tests: (equal? (cells 0) (list "0")) (equal? (cells 2) (list "2" "1" "0"))
Images have always been values in DrScheme. All we had, however, were constants. Now we also have functions for creating images:
(rectangle 10 20 'red)
(circle 10 'red)
(disk 10 'red)
(empty-scene 100 200)
(place-image (rectangle 5 5 'red) 10 20 (empty-scene 100 200))
( image→ color-list (rectangle 3 4 'green))
(make-color 60 248 52)
( color-list→ image (list (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52)) 1 12)
(make-color 60 248 52)
into a horizontal line of 12 green pixels
( color-list→ image (list (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52) (make-color 60 248 52)) 3 4)
(make-color 60 248 52)
into a horizontal line of 12 green pixels
(offset-image+ (rectangle 1 11 'red) -5 5 (rectangle 11 1 'red))
(define UFO ...) ;; some shape that resembles a green saucer ;;Number → Number;; determine the UFO’sxcoordinate at timet(define (ufo-x t) 10) ;;Number → Number;; determine the UFO’sycoordinate at timet(define (ufo-y t) (+ 20 t)) ;;Number Scene → Scene;; place the UFO intosat timet(define (place-ufo t s) (place-image UFO (ufo-x t) (ufo-y t) s)) ;; Tests: (= (ufo-x (random 100)) 10) (= (ufo-y 10) 30)
Use the stepper to see how things work.