;; 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-cond-dist) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) #| This week's outline: part01: union-types part02: structs part03: functions returning structs As time: slides about PL terms. (If you go to the Lectures page and click on the link 'lect06-dist', it looks like gobbledy-gook. I'll fix that, but for now: you can download the file, and then use DrRacket > Open File.) If you want practice writing small Java programs--functions, really -- you can look at javabat.com . |# (define n 17) (cond [(< n 10) "ehllo"] [(< n 20) (cos (+ (sqr n) -99))] [(< n 100) "woo-hoo!"]) #| #| The Design Recipe (take 1 -- primitive types only) ------- per function: 4. tests 5. stub : signature, description, header, stub-body 7. complete the body-expression 8. profit! (watch your tests pass) |# ; Task: `turn`, for changing a stop-light. ; ...wait, what data type to use? ; Data Definition: ; A slc ("stoplight color") is one of: ; - 'red, OR ; - 'yellow, OR ; - 'green, OR ; - 'flashing-yellow, OR ; - 'flashing-red ; The "OR"s mean that slc is a "union type"; ; any function handling a union-type will have a `cond` with ; as many branches as there are variants for slc. |# ; Let's write "turn", which turns the color of the stoplight. (check-expect (turn 'yellow) 'red) (check-expect (turn 'red) 'green) (check-expect (turn 'green) 'yellow) (check-expect (turn 'flashing-yellow) 'flashing-yellow) (check-expect (turn 'flashing-red) 'flashing-red) ; turn : slc -> slc ; What is the next color that will be shown, after `current-color`? (define (turn current-color) (cond [(symbol=? current-color 'red) 'green] [(symbol=? current-color 'yellow) 'red] [(symbol=? current-color 'green) 'yellow] [(symbol=? current-color 'flashing-yellow) 'flashing-yellow] [(symbol=? current-color 'flashing-red) 'flashing-red])) ; fine-for-running : slc -> non-negative real ; Determine the fine, for running a light whose color was `color-ran`. (define (fine-for-running color-ran) (cond [(symbol=? color-ran 'red) 50] [(symbol=? color-ran 'yellow) 0] [(symbol=? color-ran 'green) 0] [(symbol=? color-ran 'flashing-yellow) 20] [(symbol=? color-ran 'flashing-red) 40.37])) ; Here's a template for ANY function which takes ; a slc as input: ; (define (func-for-slc a-slc) (cond [(symbol=? a-slc 'red) ...] [(symbol=? a-slc 'yellow) ...] [(symbol=? a-slc 'green) ...] [(symbol=? a-slc 'flashing-yellow) ...] [(symbol=? a-slc 'flashing-red) ...])) (check-expect (fine-for-running 'red) 50) (check-expect (fine-for-running 'yellow) 0) (check-expect (fine-for-running 'green) 0) (check-expect (fine-for-running 'flashing-yellow) 20) (check-expect (fine-for-running 'flashing-red) 40.37) #| We actually just did several more steps of the design-recipe. ----- only do once, per data type. 1. Choose data definition 2. Give some examples of the data 3. Template: a. If handling a union type, include a cond w/ one branch per option. Note that if we write five more functions involving stoplight-colors, we only need do steps 1-3 once. (And we'll copy/paste 3a over and over, for each functions.) |# ; Step 3, TEMPLATE, for the streetlight-color data type: ; ; func-for-slc : slc -> ??? ; (define (func2-for-slc a-slc) (cond [(symbol=? a-slc 'red) ...] [(symbol=? a-slc 'yellow) ...] [(symbol=? a-slc 'green) ...] [(symbol=? a-slc 'flashing-yellow) ...] [(symbol=? a-slc 'flashing-red) ...])) ; Data definition: ; A taxable-income-entry is one of: ; - 'exempt (tax-treaty; filer doesn't need to pay any taxes) OR ; - #false (entry not filled in!) OR ; - real (their taxable-income, in USD) ; ; Examples of taxable-income-entries: #false 'exempt ; A third example: 4335 ; this is a real, so it counts as a taxabel-income-entry (third variant). ; Now, step 3: template for a funciton handling taxable-income-entries: (define (func-for-taxable-income-entry a-tie) (cond [(symbol? a-tie) ...] [(boolean? a-tie) ...] [(real? a-tie) ...])) ; We do template steps 1-3 just ONCE per data-type (taxable-income-entry, in ; case). THEN, steps 4-8 will be repeated for each function we write which ; wants to process something of that type (taxable-income-entry). ; Example: write a function which takes a taxable-income-entry, and ; estimates the tax-due: $0 if 'exempt, 15% of the taxable-income if a real number, ; and $10000 fine if they didn't enter anything. (check-expect (est-tax-due 'exempt) 0) (check-expect (est-tax-due #false) 10000) (check-expect (est-tax-due 5000) (* 5000 0.15)) (check-expect (est-tax-due 0) 0) ; est-tax-due : taxable-income-entry -> real ; Return the estimated tax due. ; (define (est-tax-due a-tie) (cond [(symbol? a-tie) 0] [(boolean? a-tie) 10000] [(real? a-tie) (* a-tie 0.15)])) ;;;;;;;;;;;;;;;;; ;; Where are we, bigger-picture? ;; Union types: a way of making new types by combining existing types. ;; Near future -- compound types (another way to make nwe types from existing). ;; intermediate future -- include self-referential types, and how to easily handle.