[plt-scheme] HTDP Excercise 14.1.5

From: Marco Morazan (morazanm at gmail.com)
Date: Fri Jun 12 09:30:49 EDT 2009

Let's take a look:

;Purpose : This function takes a family tree node and constructs an eye-color
;               list for that node ( eye-color) can occur more.
(define eye-color
  (lambda (ftn)
    (cond
      [(empty? ftn) empty]
      [else (append (list(child-eyes ftn))
                    (list(eye-color(child-father ftn)))
                    (list(eye-color(child-mother ftn))))])))

Now, let's look at the result obtained from one of your examples:

> (eye-color Gustav)
(brown (pink () ()) (blue (green () ()) (green () ())))
>

That is *not* a list of eye-color. You need the answer to be (brown
pink blue green green). Your job is not done yet (although you are
very close to the solution).

> 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.?

No, you can not use cons. Go ahead. Change append for cons. What
result do you get? Why? Go to the help desk and look at the contracts
for append and cons. You should be able to determine when you need to
use one or another.

-- 

Cheers,

Marco


Posted on the users mailing list.