;; 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-intermediate-reader.ss" "lang")((modname 18.1.10) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp"))))) ;A parent structure is ;(make-parent children name date eyes) ;where name and eyes are symbols, ;date is a number, and children is a ;list-of-children. (define-struct parent (children name date eyes)) ; ;A list-of-children is either ;1. an empty list or ;2. (cons p loc) where p is a parent ;and loc is a list-of-children. ;fun-for-parent: parent -> ??? ;Template ;(define (fun-for-parent a-parent) ; ... (parent-children a-parent) ... ; ... (parent-name a-parent) ... ; ... (parent-date a-parent) ... ; ... (parent-eyes a-parent) ...) ; ;fun-for-loc : list-of-children -> ??? ;(define (fun-for-loc a-loc) ; (cond ; [(empty? a-loc) ...] ; [else ... (first a-loc) ... ; ... (fun-for-loc (rest a-loc)) ...])) ;Third Generation (define Gustav (make-parent empty 'Gustav 1988 'brown)) ;Second Generation (define Fred (make-parent (list Gustav) 'Fred 1966 'pink)) (define Eva (make-parent (list Gustav) 'Eva 1965 'blue)) (define Dave (make-parent empty 'Dave 1955 'black)) (define Adam (make-parent empty 'Adam 1950 'yellow)) ;First Generation (define Bettina (make-parent (list Adam Dave Eva) 'Bettina 1926 'green)) (define Carl (make-parent (list Adam Dave Eva) 'Carl 1926 'green)) ;Test - All should return true ;(blue-eyed-descendant? Bettina) ;(blue-eyed-descendant? Eva) ;(not (blue-eyed-descendant? Gustav)) ;(not (blue-eyed-descendant? Adam)) ;blue-eyed-descendant? : parent -> boolean ;Given a-parent, determines whether the parent ;or any of its descendants have blue eyes. ;blue-eyed-children? : list-of-children -> boolean ;Given a-loc (list-of-children), return true if ;any parent structure within the list-of-children have blue eyes ;or if any of their descendants have blue eyes. (define (blue-eyed-descendant a-parent) (local ((define (blue-eyed-descendant? a-parent) (cond [(symbol=? (parent-eyes a-parent) 'blue) true] [else (blue-eyed-children? (parent-children a-parent))])) (define (blue-eyed-children? a-loc) (cond [(empty? a-loc) false] [else (or (blue-eyed-descendant? (first a-loc)) (blue-eyed-children? (rest a-loc)))]))) (blue-eyed-descendant a-parent)))