HtDP Solution Set

Section 15


#|
Leaf nodes are strings representing the common name of the animal.

A T-node (taxonomy node) is one of
Level or
empty

A Level is a structure
(make-level string type list-of-t-node)
|#
(define-struct level (type name children))
#|
A type is one of
'kingdom
'phylum
'class
'order
'family
'genus
'species

A list-of-t-node is one of
empty
(cons T-node list-of-t-node)

A list-of-string is one of
empty
(cons string list-of-string)
|#


Problem 1 (Solution):
 
 


Problem 2 (Solution):
;  return list of child-names of a particular type starting a node
(define (child-names atype atnode)
(cond
[(string? atnode) empty]
[(level? atnode)
(cond
[(symbol=? atype (level-type atnode))
(list-children (level-children atnode))]
[else (child-names-lotn atype (level-children atnode))])]))
 
 
;child-names-lotn : Type List-of-T-node -> List-of-String
; return list of child-names of a particular type in a list of nodes
(define (child-names-lotn atype lotn)
(cond
[(empty? lotn) empty]
[else
(append (child-names atype (first lotn))
(child-names-lotn atype (rest lotn)))]))
 
;list-children : List-of-T-node -> List-of-String
; given a List-of-T-node, return list of names of top-level nodes
(define (list-children lotn)
(cond
[(empty? lotn) empty]
[else (cons (level-name (first lotn))
(list-children (rest lotn)))]))
 
(define l1 (make-level 'kingdom "animalia" (list (make-level 'phylum "mesozoa" empty)
(make-level 'phylum "eumetazoa" empty))))
(define l2
(make-level 'kingdom "animalia"
(list (make-level 'genus "homo"
(list (make-level 'species "sapien" empty)
(make-level 'species "habilis" empty)
(make-level 'species "erectus" empty))))))
 
(child-names 'kingdom l1)
"should be"
(list "mesozoa" "eumetazoa")
 
(child-names 'phylum l1)
"should be"
empty
 


Problem 3 (Solution):
(define (is-classified? atype name atnode)
(cond
[(empty? atnode) false]
[else
(cond
[(and (symbol=? atype (level-type atnode))
(string=? name (level-name atnode)))
true]
[else (is-classified-lotn atype name (level-children atnode))])]))
 
;is-classified-lotn : Type String List-of-T-node -> Boolean
(define (is-classified-lotn atype name lotn)
(cond
[(empty? lotn) false]
[else
(or (is-classified? atype name (first lotn))
(is-classified-lotn atype name (rest lotn)))]))
 
 
(is-classified? 'phylum "eumetazoa" l1)
"should be"
true
(is-classified? 'kingdom "plantae" l1)
"should be"
false
 


Problem 4 (Solution):
;  given name of type and T-node, return type if match or false
(define (get-type name atnode)
(cond
[(empty? atnode) false]
[else
(cond
[(string=? name (level-name atnode)) (level-type atnode)]
[else
(cond
[(boolean? (get-type-lotn name (level-children atnode)))
false]
[else (get-type-lotn name (level-children atnode))])])]))
 
 
;get-type-lotn : String List-of-T-node -> Symbol or false
; given name of type and list of T-nodes, return type if match or false
(define (get-type-lotn name lotn)
(cond
[(empty? lotn) false]
[else
(cond
[(boolean? (get-type name (first lotn)))
(get-type-lotn name (rest lotn))]
[else (get-type name (first lotn))])]))
 
 
(get-type "animalia" l1) "should be" 'kingdom
(get-type "eumetazoa" l1) "should be" 'phylum
(get-type "brachae" l1) "should be" false
 
 


Problem 5 (Solution):
; given a type and T-node, return list of names associated with type
(define (get-all-of-type atype atnode)
(cond
[(empty? atnode) empty]
[else
(cond
[(symbol=? (level-type atnode) atype)
(cons (level-name atnode)
(get-all-of-type-lotn atype (level-children atnode)))]
[else (get-all-of-type-lotn atype (level-children atnode))])]))
 
;get-all-of-type-lotn : Type List-of-T-node -> (listof String)
; given a type and list of T-nodes, return list of all names associated with type
(define (get-all-of-type-lotn atype alotn)
(cond
[(empty? alotn) empty]
[else
(append
(get-all-of-type atype (first alotn))
(get-all-of-type-lotn atype (rest alotn)))]))
 
(get-all-of-type 'species empty)
"should be" empty
(get-all-of-type 'species l2)
"should be" (list "sapien" "habilis" "erectus")
 


Problem 6 (Solution):
;  return list of all species names associated with type and name in a T-node
(define (get-species-under-type atype name atnode)
(cond
[(empty? atnode) empty]
[else
(cond
[(and
(symbol=? atype (level-type atnode))
(string=? name (level-name atnode)))
(get-all-of-type 'species atnode)]
[else
(get-species-under-type-lotn atype name (level-children atnode))])]))
 
;get-species-under-type-lotn : Type String List-of-T-node -> (listof String)
; return list of all species names associated with type and name in a List-of-T-node
(define (get-species-under-type-lotn atype name alotn)
(cond
[(empty? alotn) empty]
[else
(append (get-species-under-type atype name (first alotn))
(get-species-under-type-lotn atype name (rest alotn)))]))
 
 
(get-species-under-type 'kingdom "animalia" empty)
"should be" empty
 
(get-species-under-type 'kingdom "animalia" l2)
"should be" (list "sapien" "habilis" "erectus")
 
(get-species-under-type 'kingdom "plantae" l2)
"should be" empty
 
 
 
 
 



Jamie Raymond
Matthias Felleisen
 

23 september 2002