;; 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 lect07-dist) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ; lect07 - cond and union types, cont.: taxable-income-decl; let* ; lect08 - define-struct ; lect09 - more langs; vocab; interpret vs compile ;;;;;;;;; ; `cond` -- like if-else-if. ; syntax: ; (cond [q1 a1] ; [q2 a2] ; ... ; [qn an]) ; where each q1..qn is a boolean-expression. ; A stop-light-color is one of: ; - 'red, OR ; - 'green, OR ; - 'yellow. ; 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 ;;;;;;;;;;;;;;;;; ; Update: ; a taxable-income-entry is either: ; - 'exempt (a claim that income is exempt-- tax-treaty?), OR it's ; - #f (represents no taxes filed!), OR it's ; - a number <= 9075, OR it's ; - a number > 9075 and is <= 36900, Or it's ; - a number > 36900. ; Suppose I want to write a function that takes in a taxable-income-entry. ; (but I'm not going to tell you what the function does?!). ; How will that function look? ; a `cond` with __5__ branches: ; ; func-for-tie : taxable-income-entry -> ?? ; THIS IS A TEMPLATE: ; (define (func-for-tie a-tie) (cond [(and (symbol? a-tie) (symbol=? a-tie 'exempt)) ..] [(boolean? a-tie) ..] ;; (aTie.class==Boolean.class) [(<= a-tie 9075) ..] [(and (> a-tie 9075) (<= a-tie 36900)) ..] [(> a-tie 36900) ..])) ; rich-snob? : taxable-income-entry -> boolean ; (define (rich-snob? a-tie) (cond [(and (symbol? a-tie) (symbol=? a-tie 'exempt)) #f] [(boolean? a-tie) #f] [(<= a-tie 9075) #f] [(and (> a-tie 9075) (<= a-tie 36900)) #f] [(> a-tie 36900) #t])) ; estimated-tax : taxable-income-entry -> number (define (estimated-tax a-tie) (cond [(and (symbol? a-tie) (symbol=? a-tie 'exempt)) 0] [(boolean? a-tie) 500] [(<= a-tie 9075) ..] [(and (> a-tie 9075) (<= a-tie 36900)) ..] [(> a-tie 36900) ..])) ; OR, when coming up with the data-definition for ; taxable-income-entry, perhaps this is better?: ; A taxable-income-entry-v2 is: ; - 'exempt ; - #f ; - a taxable-income ; DESIGN QUESTION: ; Should we make `tax2` call `tax` as a helper? ; I'd say, Answer: depending on our data-def'n: ; if we included 5 branches in 'taxable-income-entry', we should have 5 branches. ; Otherwise three. ; But that just begs the question: what is the right data-def'n -- ; should it have 'exempt,#f,number or should it have 5 branches? ; I tend towards the former.