-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonady1.hs
73 lines (55 loc) · 1.51 KB
/
monady1.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
import Control.Monad
newtype MaybeT m a = MaybeT { runMaybe :: m (Maybe a)}
instance (Monad m) => Monad (MaybeT m) where
return x = MaybeT $ return $ Just x
m >>= f = MaybeT $ do
x <- runMaybe m
case x of
Nothing -> return Nothing
Just x -> runMaybe $ f x
m = [[1,2,3,4]
,[2,2,3,4]
,[3,4,5,2]]
index :: (Int,Int) ->(Int,Int) ->[[a]] -> [(Int,Int,a)]
index (y0,y1) (x0,x1) ps = do
(i,xs) <- zip [y0..y1] ps
(j,x) <- zip [x0..x1] xs
return (i,j,x)
zipM ::[a] ->[b] ->Maybe [(a,b)]
zipM [] [] = Just []
zipM (a:aa) (b:bb) = case zipM aa bb of
Nothing ->Nothing
Just x ->Just $ (a,b):x
zipM _ _ = Nothing
{-
zipM1 (x:xs) (y:ys) = do
ps <- zipM xs ys
return $ (x,y):ps
-}
indexM ::(Int,Int) ->(Int,Int) ->[[a]] ->Maybe [(Int,Int,a)]
indexM (y0,y1) (x0,x1) ps = runListT $ do
(i,xs) <- ListT $ zipM [y0..y1] ps
(j,x) <- ListT $ zipM [x0..x1] xs
return (i,j,x)
--zip :: [a] -> [b] -> [(a,b)]
--zip (x:xs) (y:ys) = (x,y) : zip xs ys
newtype ListT m a = ListT { runListT ::m [a] }
{-mapM (a ->m b) ->[m a] ->m [b]
mapM f (m:ms) = do
s <- m
s1 <- f s
ss <- mapM f xs
return (s1:ss)
sequence ::[m a] ->m [a]
sequence (m:ms) = do
x <- m
xs <- sequence ms
return (x:xs)
mapM f = sequence . map (\m ->do {s <- m; return (f s)})
-}
instance (Monad m) => Monad (ListT m) where
return x = ListT $ return [x]
m >>= f = ListT $ do
xs <- runListT m
bs <- forM xs $ \x ->runListT $ f x
return $ concat bs