argon bulletin board

Факултети => Факултет по математика и информатика => Темата е започната от: Георги в 30.05.2006, 21:24:11

Титла: HtDP, Exercise 14.1.4
Публикувано от: Георги в 30.05.2006, 21:24:11
HtDP, Exercise 14.1.4 (http://www.htdp.org/2002-09-22/Book/curriculum-Z-H-19.html#%_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.
(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 (http://groups.google.com/group/comp.lang.scheme/browse_thread/thread/98b8425b8979e64e)  :-))