Haskell 编程 (2e)的第 8 章定义了数据Tree
和二分查找函数occurs
:
data Tree a = Leaf a | Node (Tree a) a (Tree a)
occurs :: Ord a => a -> Tree a -> Bool
occurs x (Leaf y) = x == y
occurs x (Node l y r) | x == y = True
| x < y = occurs x l
| otherwise = occurs x r
练习 3(第 8 章)要求重新定义occurs
并Prelude.compare
提出问题:
为什么这个新定义比原来的版本更有效率?
这里我给出我的定义:
occurs :: Ord a => a -> Tree a -> Bool
occurs x (Leaf y) = x == y
occurs x (Node l y r) = case compare x y of EQ -> True
LT -> occurs x l
GT -> occurs x r
但我看不到效率的提高。有没有?
我是不是学错了?