**Paste:**#363102**Author:**Anonymous Coward**Language:**Haskell**Channel:**-**Created:**2018-03-04 06:14:04 UTC**Revisions:**- 2018-03-04 06:21:38 UTC #363107 (diff): No title (Anonymous Coward)
- 2018-03-04 06:19:28 UTC #363105 (diff): No title (Anonymous Coward)
- 2018-03-04 06:17:06 UTC #363104 (diff): No title (Anonymous Coward)
- 2018-03-04 06:14:49 UTC #363103 (diff): No title (Anonymous Coward)
- 2018-03-04 06:14:04 UTC #363102: No title (Anonymous Coward)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | -- data Ordering = LT | EQ | GT -- together with a function -- compare :: Ord a => a -> a -> Ordering -- that decides if one value in an ordered type is less than ( LT ), equal to ( EQ ), or greater than ( GT ) another value. Using this function, redefine the function -- occurs :: Ord a => a -> Tree a -> Bool for search trees. Why is this new definition more -- efficient than the original version? data Tree a = Leaf a | Node (Tree a) a (Tree a) -- Old definition 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 -- This version is more efficient because it only requires one comparison between x and y for each node, whereas the previous version may require two occurs :: Ord a => a -> Tree a -> Bool occurs2 x (Leaf y) = x == y occurs2 x (Node l y r) = case compare x y of LT -> occurs x l EQ -> True GT -> occurs x r |