;; 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-beginner-reader.ss" "lang")((modname recursive_structures) (read-case-sensitive #t) (teachpacks ((lib "testing.ss" "teachpack" "htdp") (lib "guess.ss" "teachpack" "htdp") (lib "master.ss" "teachpack" "htdp") (lib "draw.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "testing.ss" "teachpack" "htdp") (lib "guess.ss" "teachpack" "htdp") (lib "master.ss" "teachpack" "htdp") (lib "draw.ss" "teachpack" "htdp"))))) ;; the book example Ch. 14 ;; a child structure represents a person ;; father and mother are "child" structures that ;; represent the person's parents ;; or empty if there is no data on a parent ;; name is a symbol (the person's name) ;; date is a positive integer (year of birth) ;; eyes is a symbol that represents the color of the eyes (define-struct child (father mother name date eyes)) ;; Oldest Generation: (define Carl (make-child empty empty 'Carl 1926 'green)) (define Bettina (make-child empty empty 'Bettina 1926 'green)) ;; Middle Generation: (define Adam (make-child Carl Bettina 'Adam 1950 'yellow)) (define Dave (make-child Carl Bettina 'Dave 1955 'black)) (define Eva (make-child Carl Bettina 'Eva 1965 'blue)) (define Fred (make-child empty empty 'Fred 1966 'pink)) ;; Youngest Generation: (define Gustav (make-child Fred Eva 'Gustav 1988 'brown)) ;; blue-eyed-ancestor? : ftn -> boolean ;; to determine whether a-ftree contains a child ;; structure with 'blue in the eyes field (define (blue-eyed-ancestor? a-ftree) (cond [(empty? a-ftree) false] ;; base case [else (cond [(symbol=? (child-eyes a-ftree) 'blue) true] ;; another base case ;; recursive call, father node: [(blue-eyed-ancestor? (child-father a-ftree)) true] ;; recursive call, mother node: [(blue-eyed-ancestor? (child-mother a-ftree)) true] [else false])])) (blue-eyed-ancestor? Carl) ;; expected false (blue-eyed-ancestor? Gustav) ;; expected true ;; count-persons: family tree node -> integer ;; consumes a family tree node ;; counts how many people are there in the known family tree ;; rooted in the node (define (count-persons a-tree) (cond [(empty? a-tree) 0] [else (+ 1 (count-persons (child-father a-tree)) (count-persons (child-mother a-tree)))] ) ) (count-persons Adam) ;; expected 3 (count-persons Gustav) ;; expected 5 ;; eye-colors: family tree node -> list of symbols ;; consumes a family tree node and produces a list of all eye colors ;; in the tree. An eye color may occur more than once in the list. (define (eye-colors a-tree) (cond [(empty? a-tree) empty] [else (cons (child-eyes a-tree) (append (eye-colors (child-father a-tree)) (eye-colors (child-mother a-tree))))] ) ) (eye-colors Adam) ;; expected (list 'yellow 'green 'green) (eye-colors Gustav) ;; expected (list 'brown 'blue 'green 'green 'pink)