2.3  Example: N

;; An  N (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 with k copies of a
;; examples: 
;; --- given 0 and 'z, produce empty
;; --- given 2 and '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"))

2.4  Example: More on Images

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)

create a red, 10 x 20 rectangle


(circle 10 'red)

create a red circle with radius 10


(disk 10 'red)

create a red disk of radius 10


(empty-scene 100 200)

create an empty 100 x 200 scene


(place-image (rectangle 5 5 'red) 
             10 20 
             (empty-scene 100 200))

place a red, 5 x 5 square at (10,20) into an empty scene


( image color-list 
  (rectangle 3 4 'green))

turn the green, 3 x 4 reactangle into a list of 12 copies of (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)

turn a list of 12 copies of (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)

turn a list of 12 copies of (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))

create two thin rectangles, then combine them into a red cross

2.5  Example: Image Functions

(define UFO ...) ;; some shape that resembles a green saucer 

;; Number  →  Number
;; determine the UFO’s x coordinate at time t
(define (ufo-x t) 10)

;; Number  →  Number
;; determine the UFO’s y coordinate at time t
(define (ufo-y t) (+ 20 t))

;; Number Scene  →  Scene
;; place the UFO into s at time t
(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.