-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathm.hs
61 lines (29 loc) · 813 Bytes
/
m.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
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances, ScopedTypeVariables #-}
{-# LANGUAGE UndecidableInstances #-}
-- ghci -XTypeSynonymInstances -XFlexibleInstances -XScopedTypeVariables
-- :load m
-- :t mmconcat
-- mmconcat ["Hi", "there"]
-- mmconcat [3,4,5]
-- type classes
class MyMonoid g where
mempty :: g
mappend :: g -> g -> g
instance Num a => MyMonoid a where
mempty = 0
mappend x y = x + y
instance MyMonoid String where
mempty = ""
mappend = (++)
mmconcat :: (MyMonoid g) => [g] -> g
mmconcat [] = Main.mempty
mmconcat (x:xs) = x `Main.mappend` mmconcat xs
class MyMonad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
instance MyMonad Maybe where
return = Just
(>>=) e f =
case e of
Nothing -> Nothing
Just x -> f x