Last active
June 16, 2018 11:08
-
-
Save Adirelle/2abccadfd23fc22b12c019e2b8530798 to your computer and use it in GitHub Desktop.
Pure Monad
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
import Data.Map.Strict | |
-- Monadic | |
f :: Map String Int -> String -> Maybe Int | |
f m x = do | |
y <- lookup m x | |
let y' = g y | |
y'' <- if y' > 8 | |
then Just $ y' * 10 | |
else Nothing | |
return $ y'' - 6 | |
-- Not monadic | |
f' :: Map String Int -> String -> Maybe Int | |
f' m x = | |
let | |
y = lookup m x | |
y' = case y of | |
Just z -> Just $ g z | |
Nothing -> Nothing | |
y'' = case y' of | |
Just z | z > 8 -> Just $ y * 10 | |
_ -> Nothing | |
in | |
case y'' of | |
Just z'' -> Just $ z'' - 6 | |
Nothing -> Nothing | |
-- Some pure function | |
g :: Int -> Int | |
g x = x * 46 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment