;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-advanced-reader.ss" "lang")((modname lect06-descendent-tree) (read-case-sensitive #t) (teachpacks ((lib "world.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "world.ss" "teachpack" "htdp"))))) ;The steps of design recipe (taken from How to Design Programs). ;1. Data Def'n ;2. Examples of Data ;3. Template for functions handling the data ;4a Write function header ; b Write function comments ; c Write test cases ; d Inventory the pieces of info available: ; i. a cond line for each variant of union data ; ii. pull out fields for each line handling a struct. (Types?) ; iii. add natural-recursion call, if present. (Type returned?) ; iv. Add code to combine the above pieces. ;5. run tests ;;; A WHOLE NEW PROBLEM -- DIFFERENT DATA-DEFN FROM PREVIOUS ANCESTOR-TREE EXAMPLE! ;; *Descendent* Trees / directory-processing; ; A list-of-parent is ; - empty ; - (cons [parent] [list-of-parent]) ; A parent is: ; (make-parent [symbol] [int] [list-of-parent]) (define-struct parent (name year kids)) (define bart (make-parent 'Bart 1979 empty)) (define lisa (make-parent 'Lisa 1981 empty)) (define maggie (make-parent 'Maggie 1988 empty)) (define homer (make-parent 'Homer 1955 (list bart lisa maggie))) (define marge (make-parent 'Marge 1956 (list bart lisa maggie))) (define selma (make-parent 'Selma 1950 empty)) (define patty (make-parent 'Patty 1950 empty)) (define jackie (make-parent 'Jackie 1926 (list selma patty marge))) (define herbert (make-parent 'Herbert 1950 empty)) (define mona (make-parent 'Mona 1929 (list herbert homer))) (define abe (make-parent 'Abe 1920 (list homer herbert))) ; Note that we can have dags, as well as trees: (define adam (make-parent 'Adam -6000 (list abe mona))) ; Write: ; born-between : parent num num -> (list-of parent) ; Return all descendents of p born in [start,stop). ; (define (born-between p start stop) (if (< start (parent-year p) stop) (cons p (all-born-between (parent-kids p) start stop)) (all-born-between (parent-kids p) start stop))) ; (list-of parent) num num -> (list-of parent) (define (all-born-between lop start stop) (cond [(empty? lop) empty] [(cons? lop) (append (born-between (first lop) start stop) (all-born-between (rest lop) start stop))])) (check-expect (born-between bart 1920 1950) empty) (check-expect (born-between bart 1920 1990) (list bart)) #| Quote. Sexpr. (see GlobalPreferences.plist.orig write filter (passing in a list) map, filter, fold(reduce); See: http://labs.google.com/papers/mapreduce.html writing loop; |#