• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Oct 13th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. --Matthew Larson Exercise 5.1 10/14/2019
2. --Parsiminous.hs
3.
4. import Prelude
5.
6. data Pair a b = Pair a b
7.   deriving (Eq, Show)
8.
9. instance (Ord a, Ord b) => Ord(Pair a b) where
10.   Pair a1 b1 <= Pair a2 b2 =  a1 <= a2 && b1 <= b2
11.
12.
13. --Matthew Larson Exercise 5.3 10/14/2019
14. --BinaryTree.hs
15.
16. import Prelude
17. import Data.Foldable
18.
19.
20. data BinaryTree a
21.         = EmptyTree
22.         | Node a (BinaryTree a) (BinaryTree a) -- value leftChild rightChild
23.         deriving (Show)
24.
25.
26.
27.
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))
37.
38. instance Foldable BinaryTree where
39.   foldr combiner base EmptyTree = base
40.   foldr combiner base (Node a left right) =
41.           foldr combiner (combiner a (foldr combiner base right)) left
42.
43.
44.
45. reverseTree :: BinaryTree a -> [a]
46. reverseTree EmptyTree = []
47. reverseTree (Node a left right) = reverseTree right ++ [a] ++ reverseTree left
48.
49. visit :: BinaryTree a -> [a]
50. visit = foldr (:) []
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?