【发布时间】:2014-08-08 09:38:20
【问题描述】:
我马上要参加考试,需要有关家谱的考试问题的帮助。我以前做过树,但只有这种格式:
data Tree a = Empty | Leaf a | Node a (Tree a) (Tree a)
所以基本上是一棵树,它要么是空的,要么是叶子节点,或者一个节点有 2 棵树递归地跟随它。 现在我收到了这个问题:
DAN=DORIS
/ | \
JACK-PEGGY CHRISTINE-PAUL PHIL-JILL
/ | \ / / | \
JENNIFER LILLIAN TONY SCULA KENTON DAVID ELIZABETH
This shows that Dan married Doris and their children were Jack (who married Peggy),
Christine (who married Paul) and Phil (who married Jill). The children of Jack and Peggy
were Jennifer, Lillian and Tony. Christine and Paul had no children. Phil and Jill’s
children were Shula, Kenton, David and Elizabeth.
Assume that any name appears in the tree at most once. Neglect the possibility of
second marriages.
(i) Give a Haskell algebraic datatype definition for family trees, and show how the
above tree would be represented using your datatype
(ii) Using your datatype definition, write a Haskell function which, given the name
of a person and a family tree, returns the names of that person’s children, if
any.
对不起,这是我能画出的最好的纸上的树 - 但很明显,丹与最顶层的多丽丝结婚,然后有 3 个孩子杰克、克里斯汀和菲尔等。
所以我使用的树类型不能在这里使用,我想知道这次是什么类型定义(基本上是问题2.(i),以及(ii)的任何想法?
【问题讨论】:
-
你提到的
Tree为什么不能工作?您可以将这对夫妇视为单个值并搜索匹配项。 -
但是当它到达 Jack 和 Peggy 的孩子时呢,他们不是有 3 个孩子吗,那是 3 个子树,而我了解到的是 2 个。Phil 和 Jil 也有 4 个子树,我对 haskell 比较陌生,所以我需要更多的指导。