;; 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 lect05) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ; `cond` -- like if-else-if. ; syntax: ; (cond [q1 a1] ; [q2 a2] ; ... ; [qn an]) ; where each q1..qn is a boolean-expression. (define sl 'green) ; sl for stop-light ; turn : symbol -> symbol ; Give the next stop-light color, after turning. (define (turn sl) (cond [(symbol=? sl 'red) 'green] [(symbol=? sl 'yellow) 'red] [(symbol=? sl 'green) 'yellow])) (check-expect (turn 'red) 'green) (check-expect (turn 'green) 'yellow) (check-expect (turn 'yellow) 'red) #| The Design Recipe (take 1 -- primitive types only) ------- per function: 4. tests 5. stub : signature, header, description, stub-body 6. If your data-type was a union, have a cond with one branch per variant. 7. complete the body-expression 8. watch your tests pass |# ; Task: Write a function to return the tax-due, given taxable-income. ; (for single tax-payers; we'll stop after 4 brackets) ; ($9075 => 10%; $36900 => 15%; else 25% ) ; http://www.forbes.com/sites/kellyphillipserb/2013/10/31/irs-announces-2014-tax-brackets-standard-deduction-amounts-and-more/ (check-expect (tax 0) 0) (check-expect (tax 10) 1) (check-expect (tax 9000) 900) (check-expect (tax 9076) (+ 907.5 0.15)) (check-expect (tax 36900) (+ 907.5 (* 0.15 (- 36900 9075)))) (check-expect (tax 50000) (+ 907.5 (* 0.15 (- 36900 9075)) (* 0.25 (- 50000 36900)))) ; a taxable-income is either: ; - a number <= 9075, OR it's ; - a number > 9075 and is <= 36900, Or it's ; - a number > 36900. ; tax : number -> number ; return the tax-due, given taxable-income. ; (for single tax-payers; we'll stop after 4 brackets) (define (tax income) (cond [(<= income 9075) (* income 0.10)] [(<= income 36900) (+ 907.5 (* 0.15 (- income 9075)))] [else (+ 907.5 (* 0.15 (- 36900 9075)) (* 0.25 (- income 36900)))])) ; and : boolean boolean -> boolean ; Recall: what is the signature of "<" -- ; < : number number -> boolean