Skip to content

Instantly share code, notes, and snippets.

@Adirelle
Last active June 16, 2018 11:08
Show Gist options
  • Save Adirelle/2abccadfd23fc22b12c019e2b8530798 to your computer and use it in GitHub Desktop.
Save Adirelle/2abccadfd23fc22b12c019e2b8530798 to your computer and use it in GitHub Desktop.
Pure Monad
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