;; 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-advanced-reader.ss" "lang")((modname portfolio1) (read-case-sensitive #t) (teachpacks ((lib "testing.ss" "teachpack" "htdp"))) (htdp-settings #8(#t constructor repeating-decimal #t #t none #f ((lib "testing.ss" "teachpack" "htdp"))))) ;; Data definitions ;; A Radio Show (RS) is make-rs String Number [Listof Ad] (define-struct rs (name minutes rating timeSlot ads)) ;; An Ad is (make-ad String Number Number) (define-struct ad (name minutes profit)) ;; Examples of data: (define ipod-ad (make-ad "ipod" 2 100)) (define ms-ad (make-ad "ms" 1 500)) (define xbox-ad (make-ad "xbox" 2 300)) (define news-ads (list ipod-ad ms-ad ipod-ad xbox-ad)) (define game-ads (list ipod-ad ms-ad ipod-ad ms-ad xbox-ad ipod-ad)) (define bad-ads (list ipod-ad ms-ad ms-ad ipod-ad xbox-ad ipod-ad)) (define news (make-rs "news" 60 5 2 news-ads)) (define game (make-rs "game" 120 7 4 game-ads)) ;; compute the total time for all ads in the given list ;; total-time: [Listof Ad] -> Number (define (total-time adlist) (cond [(empty? adlist) 0] [else (+ (ad-minutes (first adlist)) (total-time (rest adlist)))])) (check-expect (total-time news-ads) 7) (check-expect (total-time game-ads) 10) ;; how much time is there for the show itself ;; show-time: RS -> Number (define (show-time an-rs) (- (rs-minutes an-rs) (total-time (rs-ads an-rs)))) (check-expect (show-time news) 53) (check-expect (show-time game) 110) ;; total-profit: Show -> Number ;; takes a show and produces the profit of that show ;; Example: (total-profit news) -> 500 (define (total-profit show) (local [(define (profit x) (* (rs-timeSlot x) (* 0.1 (rs-rating x)) (ad-profit (first (rs-ads x)))))] (cond [(empty? (rs-ads show)) 0] [else (+ (profit show) (total-profit (make-rs (rs-name show) (rs-minutes show) (rs-rating show) (rs-timeSlot show) (rest (rs-ads show)))))]))) ;; ad-profits: [Listof ad] -> Number ;; Produces the total profit of a list of ads ;; Example: (ad-profits news-ads) -> 1000 (define (ad-profits ads) (cond [(empty? ads) 0] [(cons? ads) (+ (ad-profit (first ads)) (ad-profits (rest ads)))])) (check-expect (ad-profits news-ads) 1000) ;; ad-profits2: [Listof ad] acc -> Number ;; Uses an acc to produce total profit of a list of ads ;; Example: (ad-profits2 news-ads 0) -> 1000 (define (ad-profits2 ads acc) (cond [(empty? ads) acc] [(cons? ads) (+ acc (ad-profits2 (rest ads) (ad-profit (first ads))))])) (check-expect (ad-profits2 news-ads 0) 1000) ;; ad-profits3: [Listof ad] -> Number ;; Uses foldl to produce the total profits for a list of ads (define (ad-profits3 ads) (local [(define (f x y) (+ y (ad-profit x)))] (foldl f 0 ads))) (check-expect (ad-profits3 news-ads) 1000) ;; ad-profits4: [Listof ad] -> Number ;; Uses foldr to produce the total profits for a list of ads (define (ad-profits4 ads) (local [(define (f x y) (+ y (ad-profit x)))] (foldr f 0 ads))) (check-expect (ad-profits4 news-ads) 1000) (generate-report)