;; 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-abbr-reader.ss" "lang")((modname week5_m_full) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "2htdp"))))) ; A ftn is either ; - empty ; - (make-child name date eyes mom dad) ; where a child is ; - symbol name ; - number date (a year) ; - symbol eyes (a color) ; - ftn mom ; - ftn dad (define-struct child (name date eyes mom dad)) ; template: ; (define (fun-for-ftn a-ftree) ; (cond ; [(empty? a-ftree) ...] ; [else ; ... (child-name a-ftree) ... ; ... (child-date a-ftree) ... ; ... (child-eyes a-ftree) ... ; ... (fun-for-ftn (child-dad a-ftree)) ... ; ... (fun-for-ftn (child-mom a-ftree)) ... ;])) ; examples: ;; Grandparents (define Dave (make-child 'Dave 1937 'black empty empty)) (define Eva (make-child 'Eva 1934 'blue empty empty)) (define Fred (make-child 'Fred 1930 'pink empty empty)) (define Greta (make-child 'Greta 1933 'brown empty empty)) ;; Parents (define Carl (make-child 'Carl 1967 'green Eva Dave)) (define Bettina (make-child 'Bettina 1966 'green Greta Fred)) ;; Youngest Generation: (define Adam (make-child 'Adam 1990 'yellow Bettina Carl)) ; older: ftn ftn -> ftn ; Returns the family tree with the older youngest member ; (or empty, if both family trees are empty). ; Assumes that children are younger than their parents. (define (older f g) (cond [(empty? f) g] [(empty? g) f] [else (cond [(< (child-date f) (child-date g)) f] [else g])])) (check-expect (older empty empty) empty) (check-expect (older Carl empty) Carl) (check-expect (older empty Carl) Carl) (check-expect (older Carl Bettina) Bettina) (check-expect (older Bettina Carl) Bettina) (check-expect (older Bettina Greta) Greta) ; payroll-expense: list-of-numbers[rate] list-of-numbers[hours] -> number ; computes the total amount paid to workers ; given wages (rate) and hours worked (hours) ; rate and hours are restricted to be the same length ; (define (fun-for-list-of-numbers numlist) ; (cond [(empty? numlist) ...] ; [(cons? numlist) ... (first numlist) ... (fun-for-list-of-numbers (rest numlist)) ... ]) (define (payroll-expense rate hours) (cond [(empty? rate) 0] [else (+ (* (first rate) (first hours)) (payroll-expense (rest rate) (rest hours)))] )) (check-expect (payroll-expense (list 8 8 15) (list 120 100 200)) 4760) (check-expect (payroll-expense empty empty) 0) ; four options for recursive calls: ; (fun-for-two-lists list1 list2) -- problematic! ; (fun-for-two-lists (rest list1) list2) ; (fun-for-two-lists list1 (rest list2)) ; (fun-for-two-lists (rest list1) (rest list2)) -- what we saw in payroll-expense ; intersect : list-of-numbers list-of-numbers -> list-of-numbers ; returns a list of the numbers common to two input lists ; assumes individual lists have no duplicate members (define (intersect lon1 lon2) (cond [(empty? lon1) empty] [(empty? lon2) empty] [else ; (cond [(member? (first lon1) lon2) ; (cons (first lon1) ; (intersect (rest lon1) lon2))] ; [else (intersect (rest lon1) lon2)])]) (append (cond [(member? (first lon1) lon2) (list (first lon1))] [else empty]) (intersect (rest lon1) lon2))]) ) (check-expect (intersect (list 1 34 20 2) (list 34 2 102 5)) (list 34 2)) (check-expect (intersect (list 1 20 2 5 102) (list 34 2 102 5)) (list 2 5 102)) ; note -- the outputs here are sorted in the order returned by my program, but this isn't strictly required. A sort would provide more accurate tests. ; intersect-fast : list-of-numbers list-of-numbers -> list-of-numbers ; returns a list of the numbers common to two input lists ; assumes individual lists are sorted in ascending order (define (intersect-fast lon1 lon2) (cond [(empty? lon1) empty] [(empty? lon2) empty] [else (cond [(= (first lon1) (first lon2)) (cons (first lon1) (intersect-fast (rest lon1) (rest lon2)))] [(< (first lon1) (first lon2)) (intersect-fast (rest lon1) lon2) ] [else (intersect-fast lon1 (rest lon2))])])) (check-expect (intersect-fast (list 1 2 20 34) (list 2 5 34 102)) (list 2 34)) (check-expect (intersect-fast (list 1 2 5 20 102) (list 2 5 102)) (list 2 5 102)) (check-expect (intersect-fast (list 2 5 102) (list 1 2 5 20 102)) (list 2 5 102)) (check-expect (intersect-fast (list 1 2 3) (list 1 2 3)) (list 1 2 3)) (check-expect (intersect-fast (list 1 2 5 20 102) empty) empty)