;; Wednesday, Sept 28th ;; Want to keep track of each animal's name and species ;; Want to keep track of each zookeeper's name and two assigned animals (define-struct animal (name species)) (define-struct zookeeper (name animal1 animal2)) (define a1 (make-animal "Tony" "tiger")) (define a2 (make-animal "Ed" "horse")) (define a3 (make-animal "Bugs" "rabbit")) (define zk1 (make-zookeeper "Tyler Aldrich" a3 a1)) ; Want zookeeper's name (zookeeper-name zk1) ; Want *species* of the zookeeper's *second animal* (animal-species (zookeeper-animal2 zk1)) ;; Topic: unions #| ------------------------------------------------------------ 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., 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. Racket:". Problem 3: Design a function that counts the characters in a customer record. |# (define-struct dr (first last)) (define-struct mr (first last)) (define-struct ms (first last)) ;; A CUSTOMER is one of: ;; -- (make-dr String String) ;; -- (make-mr String String) ;; -- (make-ms String String) ;; Note: A CUSTOMER is a union, NOT an enumeration (define c1 (make-dr "Amal" "Ahmed")) (define c2 (make-mr "James" "Klein")) (define c3 (make-ms "Lahiru" "Dayananda")) ;; contract: Customer -> String ;; purpose: takes customer record and produces a formal greeting ;; examples ;; (make-dr "Amal" "Ahmed") ---> "Dear Dr. Ahmed:" ;; (make-mr "James" "Klein") ---> "Dear Mr. Klein:" ;; (make-ms "Lahiru" "Dayananda") ---> "Dear Ms. Dayananda:" ;; template (define (cust-template cust) (cond [(dr? cust) (... (dr-first cust) ... (dr-last cust) ... )] [(mr? cust) (... (mr-first cust) ... (mr-last cust) ... )] [else (... (ms-first cust) ... (ms-last cust) ... )])) ;; code (define (greeting cust) (cond [(dr? cust) (string-append "Dear Dr. " (dr-last cust) ":")] [(mr? cust) (string-append "Dear Mr. " (mr-last cust) ":")] [else (string-append "Dear Ms. " (ms-last cust) ":")])) ;; tests (check-expect (greeting (make-dr "Amal" "Ahmed")) "Dear Dr. Ahmed:") (check-expect (greeting (make-mr "James" "Klein")) "Dear Mr. Klein:") (check-expect (greeting (make-ms "Lahiru" "Dayananda")) "Dear Ms. Dayananda:") ;;------------------------------------------------------------------ ;; Thursday, Sept 29th ;; And Many More Unions ;; Problem: We have interns, who work at an hourly rate, and ;; full-time employees, who are salaried. We need to keep track ;; of these employees, and print out their paychecks once a ;; month: "Pay to the order of Olin Shivers, $15000." ;; An Employee is one ;; -- Intern ;; -- Fulltime (define-struct name (first last)) ;; A Name is (make-name String String) (define-struct intern (name wage hours)) ;; An Intern is (make-intern Name Number Number) ;; where name is the intern's name, wage is the hourly wage and ;; hours is the hours worked per week (define-struct fulltime (name salary)) ;; A Fulltime is (make-fulltime Name Number) ;; where name is the name struct of the employee and salary is the monthly ;; print-paycheck : Employee -> String ;; consumes an employee and prints out the monthly paycheck: ;; "Pay to the order of ." ;; template (define (emp-template emp) (cond [(intern? emp) (... (intern-name emp) ... (intern-wage emp) ... (intern-hours emp) ...)] [else (... (fulltime-name emp) ... (fulltime-salary emp) ...) ])) ;; code #; (define (print-paycheck emp) (cond [(intern? emp) (string-append "Pay to the order of " (name-first (intern-name emp)) " " (name-last (intern-name emp)) ", $" (number->string (* 4 (intern-wage emp) (intern-hours emp))) ".")] [else (string-append "Pay to the order of " (name-first (fulltime-name emp)) " " (name-last (fulltime-name emp)) ", $" (number->string (fulltime-salary emp)) ".")])) ;; monthly-pay : Employee -> Number (define (monthly-pay emp) (cond [(intern? emp) (* 4 (* (intern-wage emp) (intern-hours emp)))] [else (fulltime-salary emp)])) ;; fullname : Employee -> String (define (fullname emp) (cond [(intern? emp) (string-append (name-first (intern-name emp)) " " (name-last (intern-name emp)))] [else (string-append (name-first (fulltime-name emp)) " " (name-last (fulltime-name emp)))])) (define (print-paycheck emp) (string-append "Pay to the order of " (fullname emp) ", $" (number->string (monthly-pay emp)) ".")) (check-expect (print-paycheck (make-fulltime (make-name "Olin" "Shivers") 15000)) "Pay to the order of Olin Shivers, $15000.") (check-expect (print-paycheck (make-intern (make-name "Peter" "Griffin") 20 30)) "Pay to the order of Peter Griffin, $2400.")