forked from psosera/pli-notes
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path09-TypeclassesAndFunctors.hs
62 lines (46 loc) · 1.39 KB
/
09-TypeclassesAndFunctors.hs
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module TypeclassesAndFunctors where
-- TODO: write me!
class Prettyable a where
pretty :: a -> String
instance (Prettyable Integer) where
pretty n = "!!" ++ show n ++ "!!"
data Exp
= EInt Int
| EPlus Exp Exp
deriving (Show, Eq)
instance Prettyable Exp where
pretty (EInt n) = show n
pretty (EPlus e1 e2) = "(" ++ pretty e1 ++ " + " ++ pretty e2 ++ ")"
prettyExample :: String
prettyExample = pretty (5 :: Integer) ++ " and " ++ pretty (EPlus (EInt 1) (EInt 1))
instance Prettyable a => Prettyable (Maybe a) where
pretty Nothing = "{}"
pretty (Just v) = "{" ++ pretty v ++ "}"
-- class Functor f where
-- fmap :: (a -> b) -> f a -> f b
-- instance Functor Maybe where
-- fmap f Nothing = Nothing
-- fmap f (Just x) = Just $ f x
-- instance Functor [] where
-- fmap f [] = []
-- fmap f (x:xs) = f x :: fmap f xs
data Tree a
= Leaf
| Node (Tree a) a (Tree a)
deriving (Show, Eq)
instance Functor Tree where
fmap f Leaf = Leaf
fmap f (Node l v r) = Node (fmap f l) (f v) (fmap f r)
-- instance Functor ((->) a) where
-- fmap f g = f . g
-- class Functor f => Applicative f where
-- pure :: a -> f a
-- ap :: f (a -> b) -> f a -> f b
-- instance Applicative Maybe where
-- pure = Just
-- ap Nothing _ = Nothing
-- ap _ Nothing = Nothing
-- ap (Just f) (Just x) = Just $ f x
-- instance Applicative [] where
-- pure v = [v]
-- ap fs xs = [f x | f <- fs, x <- xs]