;;;; Brand ;;;; (define-struct brand (type speed seats service)) ;; A brand is a structure ;; (make-brand ) ;; where type is the model/make, speed is the cruising speed, ;; seats is the seating capacity, and service is the number ;; of miles before an overhaul is required. ;; type is the mode ; Examples of the data: ; (define dc10 (make-brand 'DC-10 550 282 15000)) (define md80 (make-brand 'MD-80 505 141 10000)) (define atr72 (make-brand 'ATR-72 300 46 5000)) (define (is-dc10-brand? a-brand) (symbol= 'DC-10 (brand-type a-brand))) ;;;; Plane ;;;; (define-struct plane (brand miles mechanic)) ;; A plane is a structure ;; (make-plane ) ;; where brand is a brand structure, ;; miles is the number of miles flown since the last overhaul, and ;; mechanic is the name of the mechanic who served you. (define plane1 (make-plane dc10 1000 'Joe)) (define plane2 (make-plane md80 500 'Jane)) #| Template: ;; plane-func: plane --> ?? ;; ;; (define (plane-func a-plane ...) ..(plane-brand a-plane).. ..(plane-miles a-plane).. ..(plane-mechanic a-plane).. ) |# ;; is-dc10?: plane --> boolean ;; Is thie plane's brand's type 'DC-10? ;; (define (is-dc10? a-plane) (is-dc10-brand? (plane-brand a-plane))) (boolean=? (is-dc10? plane1) true) ; Equivalanetly, (is-dc110? plane1). (boolean=? (is-dc10? plane2) false) ; Equivalanetly, (not (is-dc110? plane2)). ;;;; A List of Plane ;;;; ;; A list-of-plane is ;; - empty, or ;; - (cons ) ; Examples of list-of-plane: (define lop0 empty) (define lop1 (cons plane1 empty)) (define lop2 (cons plane2 (cons plane1 empty))) (define lop3 (cons plane1 (cons plane2 (cons plane1 empty)))) #| Template for list-of-planes: ;; lop-func: list-of-planes --> ?? ;; ;; (define (lop-func a-lop) (cond [(empty? a-lop) ...] [(cons? a-lop) ..(first a-lop)..(lop-func (rest a-lop))..])) |# ;; count-dc10s: list-of-planes --> num ;; Given a list of planes, return how many of them are DC-10s. ;; (define (count-dc10s a-lop) (cond [(empty? a-lop) ...] [(cons? a-lop) ..(first a-lop)..(count-dc10s (rest a-lop))..])) (= (count-dc10s lop0) ..) (= (count-dc10s lop1) ..) (= (count-dc10s lop2) ..) (= (count-dc10s lop3) ..) ;; just-dc10s: list-of-planes --> list-of-planes ;; Given a list of planes, return all the ones that are DC-10s. ;; (define (just-dc10s a-lop) (cond [(empty? a-lop) ...] [(cons? a-lop) ..(first a-lop)..(just-dc10s (rest a-lop))..])) ;;;; Mixed Data ;;;; #| A list-of-nums-and-syms is one of - empty - (cons S lons) where S is a symbol and lons is a list-of-nums-and-syms - (cons N lons) where N is a symbol and lons is a list-of-nums-and-syms |# ; Example of data: empty (define raw-mallow (cons 'marshmallow empty)) (define roast-mallow (cons 'marshmallow (cons 1 empty))) (define stir-fry (cons 'onions (cons 'garlic (cons 2 (cons 'tofu (cons 8 (cons 'spinach (cons 1 empty)))))))) (define half-baked (cons 'nuts (cons 3 (cons 'salt (cons 1 (cons 'nuts empty)))))) #| Template for list-of-nums-and-syms (define (fun a-lons ..) (cond [(empty? a-lons) ..] [(symbol? (first a-lons)) ..(first a-lons).. ..(fun (rest a-lons)).. ] [(number? (first a-lons)) ..(first a-lons).. ..(fun (rest a-lons)).. ])) |# ;; cook-time : lons -> num ;; sum up all the numbers in the list ;; (define (cook-time a-lons) (cond [(empty? a-lons) ..] [(symbol? (first a-lons)) ..(first a-lons).. ..(cook-time (rest a-lons)).. ] [(number? (first a-lons)) ..(first a-lons).. ..(cook-time (rest a-lons)).. ])) (= (cook-time empty) ..) (= (cook-time raw-mallow) ..) (= (cook-time roast-mallow) ..) (= (cook-time stir-fry) ..) (= (cook-time half-baked) ..) ;; ingredient-count : lons -> num ;; count how many symbols are in the list ;; (define (ingredient-count a-lons) (cond [(empty? a-lons) ..] [(symbol? (first a-lons)) ..(first a-lons).. ..(ingredient-count (rest a-lons)).. ] [(number? (first a-lons)) ..(first a-lons).. ..(ingredient-count (rest a-lons)).. ])) (= (ingredient-count empty) ...) (= (ingredient-count raw-mallow) ...) (= (ingredient-count roast-mallow) ...) (= (ingredient-count stir-fry) ...) (= (ingredient-count half-baked) ...) ;; no-cook-recipe? : lons -> bool ;; returns true if there are no numbers in the list ;; (define (no-cook-recipe? a-lons) (cond [(empty? a-lons) ..] [(symbol? (first a-lons)) ..(first a-lons).. ..(no-cook-recipe? (rest a-lons)).. ] [(number? (first a-lons)) ..(first a-lons).. ..(no-cook-recipe? (rest a-lons)).. ])) (boolean=? (no-cook-recipe? empty) ...) (boolean=? (no-cook-recipe? raw-mallow) ...) (boolean=? (no-cook-recipe? roast-mallow) ...) (boolean=? (no-cook-recipe? stir-fry) ...) (boolean=? (no-cook-recipe? half-baked) ...) ;; get-ingredients : lons -> list-of-symbols ;; extracts a list of the symbols in the list ;; (define (get-ingredients a-lons) (cond [(empty? a-lons) ..] [(symbol? (first a-lons)) ..(first a-lons).. ..(get-ingredients (rest a-lons)).. ] [(number? (first a-lons)) ..(first a-lons).. ..(get-ingredients (rest a-lons)).. ])) (get-ingredients empty) = ... (get-ingredients raw-mallow) = ... (get-ingredients roast-mallow) = ... (get-ingredients stir-fry) = ... (get-ingredients half-baked) = ... ;; substitute : lons sym sym -> lons ;; replaces all uses of the first symbols with the second symbol ;; (define (substitute a-lons old-sym new-sym) (cond [(empty? a-lons) ..] [(symbol? (first a-lons)) ..(first a-lons).. ..(substitute (rest a-lons)).. ] [(number? (first a-lons)) ..(first a-lons).. ..(substitute (rest a-lons)).. ])) (substitute empty 'msg 'salt) = ... (substitute raw-mallow 'marshmallow 'egg) = ... (substitute roast-mallow 'marshmallow 'egg) = ... (substitute stir-fry 'tofu 'ground-chuck) = ... (substitute half-baked 'nuts 'nvts) = ...