Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Gabe Schoenbach
  2.  
  3. module TypeClasses where
  4.  
  5.   import Data.Foldable
  6.  
  7.   -- Exercise 5.1
  8.  
  9.   -- | A parsimonious instance of Ord for suitably type-constrained instances of Pair
  10.   data Pair a b = Pair a b
  11.     deriving (Eq, Show)
  12.  
  13.   instance (Ord a, Ord b) => Ord (Pair a b) where
  14.     Pair a1 b1 <= Pair a2 b2 = a1 < a2 || (a1 == a2 && b1 <= b2)
  15.  
  16.   -- Exercise 5.3
  17.  
  18.   -- | The BinaryTree datatype
  19.   data BinaryTree a
  20.         = EmptyTree
  21.         | Node a (BinaryTree a) (BinaryTree a) -- value leftChild rightChild
  22.         deriving (Show)
  23.  
  24.   -- | Creates a BinaryTree that is a mirror image of the input BinaryTree.
  25.   reverseTree :: BinaryTree a -> BinaryTree a
  26.   reverseTree EmptyTree = EmptyTree
  27.   reverseTree (Node v l r) = Node v (reverseTree r) (reverseTree l)
  28.  
  29.   tree :: BinaryTree Integer
  30.   tree = Node 3
  31.           (Node 1
  32.               (Node 0 EmptyTree EmptyTree)
  33.               (Node 2 EmptyTree EmptyTree))
  34.           (Node 5
  35.               (Node 4 EmptyTree EmptyTree)
  36.               (Node 6 EmptyTree EmptyTree))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement