1

我正在读这本书,我遇到了一个例子。这确定了 a-ftree 是否包含在眼睛字段中具有 'blue 的子结构。

(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))


;; blue-eyed-ancestor? : ftn  ->  boolean
;; to determine whether a-ftree contains a child
;; structure with 'blue in the eyes field
;; version 2: using an or-expression
(define (blue-eyed-ancestor? a-ftree)
  (cond
    [(empty? a-ftree) false]
    [else (or (symbol=? (child-eyes a-ftree) 'blue)
              (or (blue-eyed-ancestor? (child-father a-ftree))
                  (blue-eyed-ancestor? (child-mother a-ftree))))]))

我想知道您将如何重新制作该函数,以便它可以确定 a-ftree 在日期字段中是否包含出生日期为 1966 的孩子?

4

1 回答 1

1

它与您已经拥有的非常相似。这是一般的想法:

; birth-date? returns true if there's a child in the tree with the given date
;   a-ftree: a family tree
;   date:    the date we're looking for
(define (birth-date? a-ftree date)
  (cond
    [<???> <???>]                         ; identical base case
    [else (or (= (<???> a-ftree) <???>)   ; if this child has the expected date
              (or (<???> <???> date)      ; advance the recursion over father
                  (<???> <???> date)))])) ; advance the recursion over mother

像这样使用它:

(birth-date? Gustav 1966)
=> #t
于 2013-02-24T23:09:39.477 回答