Last active
August 6, 2017 05:37
-
-
Save btipling/573d109d6e89f61a0c58c4eb1cba178b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Control.Monad.Writer | |
main :: IO () | |
main = do | |
putStrLn $ "do notation: " ++ (show $ runWriter multWithLog) | |
putStrLn $ "binding: " ++ (show $ runWriter multiWithLogBind) | |
putStrLn $ "let and bind: " ++ (show $ runWriter multiWithLogLet) | |
logNumber :: Int -> Writer [String] Int | |
logNumber x = writer (x, ["Got number: " ++ show x]) | |
multWithLog :: Writer [String] Int | |
multWithLog = do | |
logNumber 3 | |
let foo = "lol" | |
tell [foo] | |
logNumber 5 | |
multiWithLogBind :: Writer [String] Int | |
multiWithLogBind = ((logNumber 3) >>= (\_ -> let foo = "lol" in (tell [foo] >>= (\_ ->logNumber 5)))) | |
multiWithLogLet :: Writer [String] Int | |
multiWithLogLet = let | |
start = logNumber 3 | |
foo = "lol" | |
next = start >>= (\_ -> tell [foo]) | |
in (next >>= (\_ -> logNumber 5)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
do notation: (5,["Got number: 3","lol","Got number: 5"]) | |
binding: (5,["Got number: 3","lol","Got number: 5"]) | |
let and bind: (5,["Got number: 3","lol","Got number: 5"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment