Skip to content

Instantly share code, notes, and snippets.

@btipling
Last active August 6, 2017 05:37
Show Gist options
  • Save btipling/573d109d6e89f61a0c58c4eb1cba178b to your computer and use it in GitHub Desktop.
Save btipling/573d109d6e89f61a0c58c4eb1cba178b to your computer and use it in GitHub Desktop.
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))
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