Module 4 Extra Materials

Note: the design recipe components are not provided; they are either provided in the slides or left as an exercise.

count-apples and count-given

(define (count-apples alos)
  (cond
    [(empty? alos) 0]
    [else (+ (cond
               [(string=? "apple" (first alos)) 1]
               [else 0])
             (count-apples (rest alos)))]))

(define (count-given alos given)
  (cond
    [(empty? alos) 0]
    [else (+ (cond
               [(string=? given (first alos)) 1]
               [else 0])
             (count-given (rest alos)))]))

my-remove-all and singles

(define (my-remove-all n alon)
  (cond
    [(empty? alon) empty]
    [(= n (first alon)) (my-remove-all n (rest alon))]
    [else (cons (first alon) (my-remove-all n (rest alon))]))

(define (singles alon)
  (cond
    [(empty? alon) empty]
    [else
      (cons (first alon)
            (remove (first alon) (singles (rest alon))))]))

portions - the correct implementation

(define (portions/total alon total)
  (cond
    [(empty? alon) empty]
    [else
      (cons (/ (first alon) total)
            (portions/total (rest alon) total))]))

(define (portions alon)
  (portions/total alon (total-list alon)))

max-list

(define (max-list lon)
  (cond [(empty? (rest lon)) (first lon)]
        [else (max (first lon) (max-list (rest lon)))]))

all-same?

(define (all-same? alist)
  (cond
    [(empty? alist) true]
    [(empty? (rest alist)) true]
    [(equal? (first alist) (first (rest alist))) (all-same (rest alist))]
    [else false]))