Skip to content

Instantly share code, notes, and snippets.

@monadplus
Last active January 14, 2020 09:19
Show Gist options
  • Save monadplus/97c1f47d03689cee903f32f7e8dadf25 to your computer and use it in GitHub Desktop.
Save monadplus/97c1f47d03689cee903f32f7e8dadf25 to your computer and use it in GitHub Desktop.
Non-algebraic op
{-# LANGUAGE FlexibleContexts #-}
-- Credit for this code to: https://github.com/pva701
module Task0
( f
, f1
, f2
)where
import Control.Monad.Except (throwError, runExceptT, catchError, MonadError, runExcept)
import Control.Monad.State (runState, modify, MonadState, runStateT)
f :: (MonadError String m, MonadState Int m) => m ()
f =
(modify (const 5) *> throwError "Nope")
`catchError` \_ -> pure ()
f1 :: (Either String (), Int)
f1 = flip runState 0 $ runExceptT f
f2 :: Either String ((), Int)
f2 = runExcept $ runStateT f 0
-- *Main> f1
-- (Right (),5)
-- *Main> f2
-- Right ((),0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment