;; The first three lines of this file were inserted by DrRacket. 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 pens-conditionals) (read-case-sensitive #t) (teachpacks ((lib "universe.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "universe.ss" "teachpack" "2htdp"))))) ;; A store sells pens for 75 cents each for fewer than 10 ;; or 50 cents each for 10 or more ;; Model the cost per pen ;; cost-per-pen number -> number ;; calculate the cost for a single pen given bulk discounts (define (cost-per-pen count) (cond [(< count 10) .75] [(>= count 10) .50])) (check-expect (cost-per-pen 10) .5) ;; The store allows printing slogans on the pens. ;; Slogans add 2 cents per letter and a $3 charge for the full order ;; order-cost number string -> number ;; calculates the price (in dollars) of an order of pens with a certain slogan ;(define (order-cost count slogan) ; (+ (* count (+ (cost-per-pen count) ; (* (string-length slogan) .02))) ; 3)) ; ;(check-expect (order-cost 10 "hi!") 8.6) ;(check-expect (order-cost 10 "") 5.0) ;; The example above fails for an empty slogan because the $3 surcharge is still applied! ;; order-cost number string -> number ;; calculates the price (in dollars) of an order of pens with a certain slogan ;(define (order-cost count slogan) ; (cond [(string=? slogan "") (* count (cost-per-pen count))] ; [else (+ (* count (+ (cost-per-pen count) ; (* (string-length slogan) .02))) ; 3)])) ; ;(check-expect (order-cost 10 "hi!") 8.6) ;(check-expect (order-cost 10 "") 5.0) ;; The example above works fine but it's redundant ;; pens-cost number string -> number ;; calculates the price (in dollars) of count pens with a certain slogan (define (pens-cost count slogan) (* count (+ (cost-per-pen count) (* (string-length slogan) .02)))) ;; surcharge string -> number ;; calculates the order surcharge (in dollars) for the given slogan (define (surcharge slogan) (cond [(string=? slogan "") 0] [else 3])) ;; order-cost number string -> number ;; calculates the price (in dollars) of an order of pens with a certain slogan (define (order-cost count slogan) (+ (pens-cost count slogan) (surcharge slogan))) (check-expect (order-cost 10 "hi!") 8.6) (check-expect (order-cost 10 "") 5.0) ;; We've refined our program so it's easier to read