Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Gabe Schoenbach
- module TypeClasses where
- import Data.Foldable
- -- Exercise 5.1
- -- | A parsimonious instance of Ord for suitably type-constrained instances of Pair
- data Pair a b = Pair a b
- deriving (Eq, Show)
- instance (Ord a, Ord b) => Ord (Pair a b) where
- Pair a1 b1 <= Pair a2 b2 = a1 < a2 || (a1 == a2 && b1 <= b2)
- -- Exercise 5.3
- -- | The BinaryTree datatype
- data BinaryTree a
- = EmptyTree
- | Node a (BinaryTree a) (BinaryTree a) -- value leftChild rightChild
- deriving (Show)
- -- | Creates a BinaryTree that is a mirror image of the input BinaryTree.
- reverseTree :: BinaryTree a -> BinaryTree a
- reverseTree EmptyTree = EmptyTree
- reverseTree (Node v l r) = Node v (reverseTree r) (reverseTree l)
- tree :: BinaryTree Integer
- tree = Node 3
- (Node 1
- (Node 0 EmptyTree EmptyTree)
- (Node 2 EmptyTree EmptyTree))
- (Node 5
- (Node 4 EmptyTree EmptyTree)
- (Node 6 EmptyTree EmptyTree))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement