-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctor.hs
More file actions
63 lines (45 loc) · 1.23 KB
/
functor.hs
File metadata and controls
63 lines (45 loc) · 1.23 KB
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
module Main where
import Prelude hiding Functor
main = putStrLn "main"
class Functor f where
fmap :: (a -> b) -> f a -> f b
instance Functor [] where
fmap = map
instance Functor Maybe where
fmap f (Just x) = Just (f x)
fmap f Nothing = Nothing
instance Functor Tree where
fmap f EmptyTree = EmptyTree
fmap f (Node x lhs rhs) =
Node (f x) (fmap f lhs) (fmap f rhs)
instance Functor (Either a) where
fmap f (Right x) = Right (f x)
fmap f (Left x) = Left x
instance Functor ((->) r) where
fmap = (.)
-- fmap f g = (\x -> f (g x))
{-
Applicative Functors
-}
-- pure (+) <*> Just 3 <*> Just 5
(<$>) :: (Functor f) => (a -> b) -> f a -> f b
f <$> x = fmap f x
-- (++) <$> Just "john" <*> Just "travolta"
-- Applicative List
instance Applicative [] where
pure x = [x]
fs <*> xs [f x | f <- fs, x <- xs]
instance Applicative ZipList where
pure x = ZipList (repeat x)
ZipList fs <*> ZipList xs = ZipList (zipWith (\f x -> f x) fs xs)
-- Applicate IO
instance Applicative IO where
pure = return
a <*> b = do
f <- a
x <- b
return $ f x
-- Applicative Functions
instance Applicative ((->) r) where
pure x = (\_ -> x)
f <*> g = \x -> f x (g x)