【发布时间】:2019-02-12 15:09:53
【问题描述】:
我需要在两个 Htree 之间进行比较,为此我实现了我自己的比较函数,它与 sortBy 一起使用,但是我想实现 Eq 和 Ord 类的派生实例,但需要涵盖所有可能的案例数量组合使其不切实际。
data Htree a b = Leaf a b
| Branch a (Htree a b) (Htree a b)
deriving (Show)
instance (Eq a) => Eq (Htree a b) where
(Leaf weight1 _ ) == (Leaf weight2 _) = weight1 == weight2
(Branch weight1 _ _) == (Leaf weight2 _) = weight1 == weight2
(Leaf weight1 _ ) == (Branch weight2 _ _) = weight1 == weight2
(Branch weight1 _ _) == (Branch weight2 _ _) = weight1 == weight2
如您所见,我只想比较 Htree 的单个部分,在实际代码中它将是一个 Integer,我需要为其编写四个案例。有没有办法概括这一点,所以我可以在一个案例中编写它? 如果我比较两个 Htree,比较它们的整数部分?
我目前用来比较两个 htree 的是:
comparison :: Htree Integer (Maybe Char) -> Htree Integer (Maybe Char) ->
Ordering
comparison w1 w2 = if(getWeight(w1) > getWeight(w2)) then GT
else if(getWeight(w1) < getWeight(w2)) then LT
else EQ
其中 getWeight 定义为:
getWeight :: Htree Integer (Maybe Char) -> Integer
getWeight(Leaf weight _) = weight
getWeight(Branch weight _ _) = weight
【问题讨论】:
-
这可能不是lawful Eq instance。假设您允许树的消费者查看除节点权重之外的任何内容,您就违反了“替代性”定律。为什么需要这些 Eq 和 Ord 实例?寻找替代方法来实现您心中的任何目标。