Skip to content

Instantly share code, notes, and snippets.

@risingBirdSong
Created July 21, 2021 22:40
Show Gist options
  • Save risingBirdSong/c20e1625152ac0367db3ba95c2ff52f6 to your computer and use it in GitHub Desktop.
Save risingBirdSong/c20e1625152ac0367db3ba95c2ff52f6 to your computer and use it in GitHub Desktop.
-- unwrapping exercise, the goal is to print the contents of the IO
newtype MyActionThing a
= MyActionThing
{ runMyActionThing :: IO a
}
action :: MyActionThing Int
newtype ExceptT e m a
= ExceptT
{ runExceptT :: m (Either e a)
}
newtype ReaderT r m a
= ReaderT
{ runReaderT :: r -> m a
}
-- >>> :t runReaderT
-- runReaderT :: ReaderT r m a -> r -> m a
newtype StateT s m a
= StateT
{ runStateT :: s -> m (a, s)
}
x
:: ReaderT Int
(ExceptT Double
(StateT Char
IO)
)
String
x = do
i <- ask
when (i == 0) $ throwError 2.3
pure (replicate i 'a')
main :: IO ()
main = do
let a :: Int -> ExceptT Double (StateT Char IO) String
a = runReaderT x
b :: ExceptT Double (StateT Char IO) String
b = y 3
c :: (StateT Char IO) (Either Double String)
c = runExceptT w
d :: Char -> IO (Either Double String, Char)
d = runStateT z
e :: IO (Either Double String, Char)
e = q 'a'
res <- r
case res of
(Left d, c) -> print d -- print out some number as an error
(Right s, c) -> print s -- big success!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment