#| Lecture 9: DESIGN PRACTICE do one problem at a time; break up file |# #| ----------------------------------------------------------------------------- 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: (equal? 'green (switch 'red)) (equal? 'yellow (switch 'green)) (equal? '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-specific-tlc) (cond [(symbol=? a-specific-tlc 'red) (circle 50 'solid 'red)] [(symbol=? a-specific-tlc 'green) (circle 50 'solid 'green)] [(symbol=? a-specific-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! (equal? (circle 50 'solid 'red) (display 'red)) (equal? (circle 50 'solid 'green) (display 'green)) (equal? (circle 50 'solid 'yellow) (display 'yellow)) #| OPTIONAL: (big-bang 100 100 3 'red) (on-redraw display) (on-tick-event switch) |# #| ============================================================================= QUIZ: Create two instances of FOO: (define-struct FOO (bar pub)) ;; FOO is (make-FOO String Number) |# #| ----------------------------------------------------------------------------- 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. 'Mrs. 'Mstr. 'Miss 'Ms |# #| 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) ...) |# ;; code: (define (formal a-customer) (string-append "Dear " (symbol->string (customer-title a-customer)) " " (customer-last a-customer) ":")) ;; test: (equal? "Dear Dr. Shivers:" (formal (make-customer 'Dr. "Olin" "Shivers"))) #| 3: contract: Customer -> Number purpose: count the characters in a customer record example: given: (make-customer 'Dr. "Olin" "Shivers") wanted: 14 template: same as above (we can save by copy/paste for methods) |# (define (count a-customer) (+ (string-length (symbol->string (customer-title a-customer))) (string-length (customer-first a-customer)) (string-length (customer-last a-customer)))) (equal? 14 (count (make-customer 'Dr. "Olin" "Shivers")))