[plt-scheme] HTDP Excercise 14.1.5

From: aditya shukla (adityashukla1983 at gmail.com)
Date: Thu Jun 11 14:07:24 EDT 2009

I am trying to solve this particular exercise from htdp and this how i have
done it.
;data definition
(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))

;A family-tree-node (short: ftn) is either

 ;  1.

   ;  empty; or

    ;  (make-child f m na da ec)
     ; where f and m are ftns, na
      ;and ec are symbols, and da is a number.
;contract of eye-color ftn(family tree node) -> list of eyes color
;Purpose :This function takes a family tree node and constructs an eye-color
list for that node ( eye-color) can occur more.
;examples empty -> empty ,carl ->green , bettina -> green ,
adam->yellow,green, green , dave->black,green,green , gustav->
;template(define eye-color (lambda (ftn) (cond [(empty? ftn )][else
(count-persons (child-father ftn)) (count-persons (child-mother  ftn))
(child-name ftn) (child-date ftn) (child-eyes ftn)])))
(define eye-color (lambda (ftn)
                      [(empty? ftn) empty]
                      [else (append(list(child-eyes
ftn))(list(eye-color(child-father ftn)))(list(eye-color(child-mother
(eye-color empty)
(eye-color Carl)
(eye-color Bettina)
(eye-color Adam)
(eye-color Gustav)

(list 'green empty empty)
(list 'green empty empty)
(list 'yellow (list 'green empty empty) (list 'green empty empty))
(list 'brown (list 'pink empty empty) (list 'blue (list 'green empty empty)
(list 'green empty empty)))

My question here is :In the question it is asked to use append , and  I am
appending lists with each other.Is this the right solution ? Also I think
cons could have been used to construct a single list for a particulat
family-tree-node then why to use append.?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090611/8fc895ac/attachment.html>

Posted on the users mailing list.