-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlivecoding2_20140710.hs
64 lines (43 loc) · 1.14 KB
/
livecoding2_20140710.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
-- class Functor m where
-- fmap :: (a -> b) -> m a -> m b
-- class (Functor m) => Monad m where
-- return :: a -> m a
-- (>>=) :: m a -> ( a -> m b ) -> m b
{-
instance Monad Maybe where
-- return :: a -> Maybe a
return x = Just x
-- f x y = x `f` y
-- (>>=) :: Maybe a -> ( a -> Maybe b ) -> Maybe b
(>>=) Nothing f = Nothing
(>>=) (Just x) f = f x
-}
-- Nil | Cons
{-[] | x:xs
fmap :: (a -> [b]) -> [a] -> [[b]]
instance Monad [] where
return x = [x]
-- (>>=) :: [a] -> ( a -> [b] ) -> [b]
(>>=) [] f = []
(>>=) ys@(x:xs) f = concat ((f x) : (fmap f xs)) :: [b]
or
= (f x) ++ (xs >>= f)
(>>=) ys f = concat (fmap f ys)
f mx@(Just x) = show x ++ show mx
concat :: [[b]] -> [b]
[ xs1, xs2, xs3 ] = [ [x1,x,y], [d,s,z] , [d,z,f] ] -> [ x1,x,y,d,s,z,d,z,f]
-}
f :: Int -> Maybe String
f x | x > 0 = Just (show x)
| otherwise = Nothing
g :: String -> Maybe Int
g x = if length x > 3 then Nothing else Just (length x)
mx :: Maybe Int
mx = Just 323434
mx2 :: Maybe Int
mx2 = Nothing
main :: IO ()
main = do
print (f 3)
print (f (-2))
print (mx >>= f >>= g)