【问题标题】:Haskell Family Tree - Exam PreparationHaskell 家谱 - 考试准备
【发布时间】: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 比较陌生,所以我需要更多的指导。

标签: haskell tree


【解决方案1】:

树中的孩子数量是可变的。标准库类型Data.Tree,对树的建模大致如下:

data Tree a = Node a [Tree a]

这也适用于您的情况,除了

  • 您需要区分已婚夫妇(有任意数量的孩子)和单身人士(没有孩子)的情况
  • 您可能还想添加一棵空树

所以,在不放弃太多的情况下,一棵树可以有三种形式:空的,已婚夫妇有任意数量的孩子,单身人士没有孩子。将最后一句话从英语翻译成 Haskell,你就完成了。

【讨论】:

  • 好的顺便说一句,提供尽可能多的帮助不是为了作业或考试,而是为了准备,所以我没有作弊哈哈。谢谢你的回答
  • @darkphoton,我们隐瞒信息不是为了防止作弊(出于某种道德理由),而是因为自己努力组装细节会比我们为您拼写出来的信息更好地帮助您学习。跨度>
  • @darkphoton,另一个想法是想出你自己的解决方案来解决这个问题,并在此处发布以获取反馈。这样你会得到很多详细的信息。
  • 好的,谢谢你的提示,稍后我会发布我的解决方案以寻求帮助
【解决方案2】:
import Data.Foldable as F

data Family a = Single a | Couple a a [Family a]
    deriving Show -- useful for ghci

isCouple :: Family a -> Bool
isCouple (Couple _ _ _) = True
isCouple _ = False

isFamily :: Eq a => a -> Family a -> Bool
isFamily n (Single a) = n == a
isFamily n (Couple a b _) = n == a || n == b

q1 :: Family String
q1 = Couple "Dan" "Doris" [Couple "Peggy" "Jack" [Single "Jennifer", Single "Lillian", Single "Tony"], Couple "Christine" "Paul" [], Couple "Phil" "Jill" [Single "Scula", Single "Kenton", Single "David", Single "Elizabeth"]]

q2 :: String -> Maybe [Family String]
q2 n = q2' n [q1]

q2' :: Eq a => a -> [Family a] -> Maybe [Family a]
q2' n f = case find (isFamily n) f of
    Just (Single _) -> Just []
    Just (Couple _ _ c) -> Just c
    _ -> case filter isCouple f of
        [] -> Nothing
        cs -> q2' n $ F.concatMap (\(Couple _ _ c) -> c) cs

【讨论】:

  • 谢谢!看过这段代码后,我可能会有几个问题
猜你喜欢
  • 2023-03-24
  • 1970-01-01
  • 2020-07-24
  • 1970-01-01
  • 2016-03-17
  • 2016-11-06
  • 1970-01-01
  • 1970-01-01
  • 2022-12-16
相关资源
最近更新 更多