Автор Тема: HtDP, Exercise 14.1.4  (Прочетена 806 пъти)

Неактивен Георги

  • Публикации: 4
HtDP, Exercise 14.1.4
« -: 30.05.2006, 21:24:11 »
HtDP, Exercise 14.1.4:
Цитат
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.
(define-struct child (father mother name date eyes))

(define Carl (make-child empty empty 'Carl 2000 'green))
(define Bettina (make-child empty empty 'Bettina 2000 'green))

(define Fred (make-child Carl Bettina 'Fred 2000 'blue))
(define Pamela (make-child empty empty 'Pamela 2000 'yellow))

(define Gustav (make-child Fred Pamela 'Gustav 2001 'yellow))
;;14.1.4
;;average-age: ftn number -> number
;;produces the average age of all people in a family tree

(define (average-age ftn now)
  (cond
    [(empty? ftn) 0]
    [else (+ (average-age (child-father ftn) now)
                 (average-age (child-mother ftn) now)
                 (- now (child-date ftn)))]))
Дотук събира възрастите:
(average-age Gustav 2006);;returns 29Как да изчислява средната възраст?

Btw, вижте и този thread  :-))