-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonad.hs
139 lines (110 loc) · 3.19 KB
/
monad.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
{-# LANGUAGE InstanceSigs #-}
import Control.Applicative
import Control.Monad
twiceWhenEven :: [Integer] -> [Integer]
twiceWhenEven xs = do
x <- xs
if even x
then [x * x, x * x]
else [x]
twiceWhenEven' :: [Integer] -> [Integer]
twiceWhenEven' xs = do
xs >>= \x ->
if even x
then [x * x, x * x]
else [x]
data Cow = Cow
{ name :: String
, age :: Int
, weight :: Int
}
deriving (Eq, Show)
noEmpty :: String -> Maybe String
noEmpty "" = Nothing
noEmpty str = Just str
noNegative :: Int -> Maybe Int
noNegative n
| n >= 0 = Just n
| otherwise = Nothing
weightCheck :: Cow -> Maybe Cow
weightCheck cow =
let w = weight cow
n = name cow
in if n == "Bess" && w > 499
then Nothing
else Just cow
mkCow :: String -> Int -> Int -> Maybe Cow
mkCow name age weight = do
validName <- noEmpty name
validAge <- noNegative age
validWeight <- noNegative weight
weightCheck $ Cow validName validAge validWeight
mkCow' :: String -> Int -> Int -> Maybe Cow
mkCow' name age weight = do
noEmpty name
>>= \validName ->
noNegative age
>>= \validAge ->
noNegative weight
>>= \validWeight ->
weightCheck $ Cow validName validAge validWeight
-- years ago
type Founded = Int
-- number of programmers
type Coders = Int
data SoftwareShop = Shop
{ founded :: Founded
, programmers :: Coders
}
deriving (Eq, Show)
data FoundedError
= NegativeYears Founded
| TooManyYears Founded
| NegativeCoders Coders
| TooManyCoders Coders
deriving (Eq, Show)
validateFounded :: Int -> Either FoundedError Founded
validateFounded foundedAgo
| foundedAgo < 0 = Left $ NegativeYears foundedAgo
| foundedAgo > 500 = Left $ TooManyYears foundedAgo
| otherwise = Right foundedAgo
validateCoders :: Int -> Either FoundedError Coders
validateCoders coders
| coders < 0 = Left $ NegativeCoders coders
| coders > 5000 = Left $ TooManyCoders coders
| otherwise = Right coders
mkSoftware :: Int -> Int -> Either FoundedError SoftwareShop
mkSoftware years coders = do
validYears <- validateFounded years
validCoders <- validateCoders coders
Right $ Shop validYears validCoders
data Either' a b = Bad a | Good b deriving (Eq, Show)
instance Functor (Either' a) where
fmap _ (Bad y) = Bad y
fmap f (Good x) = Good (f x)
instance Applicative (Either' a) where
pure x = Good x
(<*>) (Bad y) _ = Bad y
(<*>) (Good f) either = fmap f either
instance Monad (Either' a) where
return = pure
(>>=) (Bad y) _ = Bad y
(>>=) (Good x) f = f x
data Nope a = NopeDotJpg
instance Functor Nope where
fmap :: (a -> b) -> Nope a -> Nope b
fmap _ _ = NopeDotJpg
instance Applicative Nope where
pure _ = NopeDotJpg
NopeDotJpg <*> NopeDotJpg = NopeDotJpg
instance Monad Nope where
NopeDotJpg >>= _ = NopeDotJpg
j :: Monad m => m (m a) -> m a
j = join
--j = (>>= id)
l1 :: Monad m => (a -> b) -> m a -> m b
l1 = fmap
l2 :: Monad m => (a -> b -> c) -> m a -> m b -> m c
l2 = liftA2
a :: Monad m => m a -> m (a -> b) -> m b
a = flip (<*>)