-
Notifications
You must be signed in to change notification settings - Fork 39
/
Command.hs
34 lines (28 loc) · 918 Bytes
/
Command.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
{-# LANGUAGE FlexibleContexts #-}
module Command where
import Control.Monad.Writer
data Light = Light {
turnOn :: IO String
, turnOff :: IO String
}
simpleLamp = Light {
turnOn = putStrLn "The Light is on" >> return "on"
, turnOff = putStrLn "The Light is off" >> return "off"
}
flipUpCommand :: Light -> IO String
flipUpCommand = turnOn
flipDownCommand :: Light -> IO String
flipDownCommand = turnOff
storeAndExecute :: IO String -> WriterT[String] IO ()
storeAndExecute command = do
logEntry <- liftIO command
tell [logEntry]
commandDemo :: IO ()
commandDemo = do
putStrLn "Command -> higher order functions"
let lamp = simpleLamp
result <- execWriterT $
storeAndExecute (flipUpCommand lamp) >>
storeAndExecute (flipDownCommand lamp) >>
storeAndExecute (flipUpCommand lamp)
putStrLn $ "switch history: " ++ show result