Section: 8.4 Derived Instances

第 8.4 节 派生实例

We have actually been using the derived Show instances all along for printing out trees and other structures we have defined. The code in the tutorial for the Eq and Ord instance of Tree is created implicitly by the deriving clause so there is no need to write it here.

其实我们一直都在使用派生的 Show 实例来打印树和我们定义的其他结构。

教程中 Tree 的 Eq 和 Ord 实例的代码由 deriving 子句隐式生成,所以

这里无需再写出来。

data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving (Eq,Ord,Show)

Now we can fire up both Eq and Ord functions for trees:

现在我们可以启用树的 Eq 和 Ord 函数了:

tree1, tree2, tree3, tree4 :: Tree Int
tree1 = Branch (Leaf 1) (Leaf 3)
tree2 = Branch (Leaf 1) (Leaf 5)
tree3 = Leaf 4
tree4 = Branch (Branch (Leaf 4) (Leaf 3)) (Leaf 5)
e1 = tree1 == tree1
e2 = tree1 == tree2
e3 = tree1 < tree2
quicksort :: Ord a => [a] -> [a]
quicksort [] = []
quicksort (x:xs) = quicksort [y | y <- xs, y <= x] ++
                   [x] ++
                   quicksort [y | y <- xs, y > x]
e4 = quicksort [tree1,tree2,tree3,tree4]

Now for Enum:

接下来是 Enum:

data Day = Sunday | Monday | Tuesday | Wednesday | Thursday |
           Friday | Saturday     deriving (Show,Eq,Ord,Enum)
e5 = quicksort [Monday,Saturday,Friday,Sunday]
e6 = [Wednesday .. Friday]
e7 = [Monday, Wednesday ..]
e8 = [Saturday, Friday ..]