From: hWnd
Subject: HtDP, Excersise 14.1.4 (average-age)
Date: 
Message-ID: <1148935314.837969.38620@j73g2000cwa.googlegroups.com>
I can't figure out how to do excersise 14.1.4
(http://htdp.org/2003-09-26/Book/curriculum-Z-H-19.html#node_sec_14.1):

Develop the function average-age. It consumes a family tree node and
the current year. It produces the average age of all people in the
family tree.

---code---

(define-struct child (father mother name date eyes))

;; Oldest Generation
(define Carl (make-child empty empty 'Carl 2001 'green))
(define Bettina (make-child empty empty 'Bettina 2001 'green))

;; Middle Generation
(define Adam (make-child Carl Bettina 'Adam 2004 '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))

;;Here's the template:

;;14.1.4
;; average-age : ftn number -> number
;; produces the average age of all people in the family tree
(define (average-age a-ftn now)
  (cond
    [(empty? a-ftn) 0]
    [else  (- now (child-date a-ftn))
           (average-age (child-father a-ftn) now)
           (average-age (child-father a-ftn) now)
           ...]))

(average-age Adam 2006)

---code end---

I can accomplish the task with a set of auxiliary functions
(count-persons, total-age, and average-age), but I can't invent a
single function to do this.

p.s.: Please, use only the stuff HtDP has introduced so far (chapter
14)!