(require 2htdp/image) (require 2htdp/universe) #| Lecture 9: DESIGN PRACTICE Design recipe: 1 Problem Analysis & Data Definition 2 Contract, Purpose & Effect Statements, Header 3 Examples 4 Function Template 5 Function Definition 6 Tests |# #| ---------------------------------------------------------------- PROBLEM: design a simplistic simulation of a traffic light. The simulation should initially display a big red circle. After 3 seconds, it should display a big green circle, and after another 3 seconds, it should switch to a yellow circle. Then it starts all over. Problem 1: a data representation of the current state of the traffic light Problem 2: design the function SWITCH, which consumes the current state of the traffic light and produces the next one. Problem 3: design the function DISPLAY, which consumes the current state of the traffic light and produces an image of it. |# #| 1: A TLC is one of: 'red, 'green, 'yellow interpretation: the symbol represents the "bulb" that's currently visible. |# #| 2: contract: TLC -> TLC purpose: Compute the next state of the traffic light. examples: given wanted 'red 'green 'green 'yellow 'yellow 'red template: (define (tlc-template a-specific-tlc) (cond [(symbol=? a-specific-tlc 'red) ...] [(symbol=? a-specific-tlc 'green) ...] [(symbol=? a-specific-tlc 'yellow) ...])) code: |# (define (switch a-specific-tlc) (cond [(symbol=? a-specific-tlc 'red) 'green] [(symbol=? a-specific-tlc 'green) 'yellow] [(symbol=? a-specific-tlc 'yellow) 'red])) ;; tests: (check-expect 'green (switch 'red)) (check-expect 'yellow (switch 'green)) (check-expect 'red (switch 'yellow)) #| 3: contract: TLC -> Image purpose: Translate a traffic light color into a big fat circle of that color. examples: given wanted 'red a solid red circle of size 50 'green a solid green circle of size 50 'yellow a solid yellow circle of size 50 template: same as above!!! code: --- copy template --- |# (define (display.v1 a-tlc) (cond [(symbol=? a-tlc 'red) (circle 50 'solid 'red)] [(symbol=? a-tlc 'green) (circle 50 'solid 'green)] [(symbol=? a-tlc 'yellow) (circle 50 'solid 'yellow)])) ;; The template is a HELP. You may need much less! (define (display a-specific-tlc) (circle 50 'solid a-specific-tlc)) ;; Tests: yes, you can test graphical things! (check-expect (circle 50 'solid 'red) (display 'red)) (check-expect (circle 50 'solid 'green) (display 'green)) (check-expect (circle 50 'solid 'yellow) (display 'yellow)) ; (check-expect (display 'red)) (define (tlc->scene tlc) (overlay (display tlc) (empty-scene 120 120))) ; OPTIONAL: (big-bang 'red (to-draw tlc->scene) (on-tick switch 1.5)) #| ---------------------------------------------------------------- PROBLEM: Design an airline customer system. The system represents each customer via a title, first name, and last name. Airlines use the following titles: Dr., Mr., Mrs., Mstr., Miss, and Ms. The system should also support a function for writing formal letter openers and a function for measuring the number of characters in a customer record. Problem 1: Create a data definition for customer records. Problem 2: Design a function that produces a letter formal opening from a customer record. Remember that a formal letter opening is something such as "Dear Dr. Scheme:". Problem 3: Design a function that counts the characters in a customer record. |# (define-struct customer (title first last)) #| Customer = (make-customer Title String String) a Title is one of : 'dr 'mr 'ms 'master |# #| 2: Contract: Customer -> String Purpose: create a formal letter opening from the given customer record example: given: (make-customer 'dr "Olin" "Shivers") wanted: "Dear Dr. Shivers:" template: (define (customer-template a-customer) ... (customer-title a-customer) ... ... (customer-first a-customer) ... ... (customer-last a-customer) ...) |#