;; The first three lines of this file were inserted by DrScheme. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname 24.1.1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor mixed-fraction #f #t none #f ())))
; Worked exercise 24.1.1

; A built-natural is either 0 or (S built-natural).

(define-struct successor [previous])
(define (S x) ; shorter name for convenience
  (make-successor x))
(define (P x) ; shorter name for convenience
  (successor-previous x))

; Examples of built-naturals:
(define zero 0)
(define one (S 0))
(define two (S (S 0)))
(define another-one (P (S (S 0))))
(define five (S (S (S (S (S 0))))))

; spams : built-natural -> list-of-string
(check-expect (spams zero) empty)
(check-expect (spams one) (list "spam"))
(check-expect (spams (S (S (S 0)))) (list "spam" "spam" "spam"))

(define (spams n)
  (cond [(equal? n 0) empty]
        [(successor? n)
         ; n       successor           (S (S (S 0)))
         ; (P n)   built-natural          (S (S 0))
         ; (spams (P n))   list of strings (list "spam" "spam")
         ; right answer    list of strings (list "spam" "spam" "spam")
         (cons "spam" (spams (P n)))
         ]))