-
Notifications
You must be signed in to change notification settings - Fork 2
/
StateMonad.hs
48 lines (34 loc) · 911 Bytes
/
StateMonad.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
module StateMonad
(
Calculator,
)
where
import qualified Data.Strings as S
import Control.Monad.State.Lazy
type Calculator a = State a (a -> a) -- State ouput state
(~~) :: (a -> a) -> Calculator a
(~~) f = state $ \s -> (f, f s)
(~@) :: (a -> a -> a) -> a -> Calculator a
(~@) op x = (~~) (op x)
(~+) :: (Num a) => a -> Calculator a
(~+) x = (~~) (+x)
(~-) :: (Num a) => a -> Calculator a
(~-) x = (~@) (-) x
(~^) :: (Integral a, Num b) => a -> Calculator b
(~^) x = (~~) (\y -> y ^ x)
kpow :: ((a -> a) -> Calculator a) -> Integer -> ((a -> a) -> Calculator a)
-- Kleisli power operator
kpow f n | n == 0 = return
| n == 1 = f
| otherwise = (f >=> (kpow f (n-1)))
op :: (Num a) => Calculator a
op = do
(~+) 10
(~-) 1
(~~) (*2)
(~^) 2
>>=
(kpow (~~) 0)
main :: IO ()
main = do
let (_, res) = runState op 0 in putStrLn ("WoW " ++ (show res))