;; A League is one of ;; -- empty ;; -- (cons Team League) ;; Examples empty (cons (cons "red sox" empty) empty) ;; TEMPLATE for Leauge #| (define (f l) (cond [(empty? l) ... ] [(cons? l) ... (first l) ... (f (rest l)) ... ] )) |# ;; A Team is ;; -- (cons String Players) #| (define (f t) ... (first t) ... (rest t) ... ) |# ;; Examples (cons "red sox" empty) ;; A Players is one of ;; -- empty ;; -- (cons Player Players) ;; Examples empty #| (define (f ps) (cond [(empty? ps) ... ] [(cons? ps) ... (first ps) ..(f (rest ps)) ... ])) |# ;; A Player is a (make-player String Number) (define-struct player (name points)) #| (define (f p) ... (player-name p) ... ... (player-points p) ...) |# ;; Problem: Calculate all the points in a team ;; players-points: Players -> Number ;; Points for all the players (define (players-points ps) (cond [(empty? ps) 0 ] [(cons? ps) (+ (player-points (first ps)) (players-points (rest ps))) ])) ;; total-points: Team -> Number (define (total-points t) (players-points (rest t))) ;; Examples (= (total-points (cons "red sox" empty)) 0) (= (total-points (cons "red sox" (list (make-player "bob" 13)))) 13) (= (total-points (cons "red sox" (list (make-player "bob" 13) (make-player "bob" 15)))) 28) ;; Problem: League -> ListOfNumber ;; league-points: League -> Number (define (league-points l) (cond [(empty? l) empty ] [(cons? l) (cons (total-points (first l)) (league-points (rest l))) ] )) ;; Examples (equal? (league-points empty) empty) (equal? (league-points (list (cons "red sox" (list (make-player "bob" 1) (make-player "bob2" 2))) (cons "white sox" (list (make-player "bob3" 3) (make-player "bob5" 4))))) (list 3 7))