Skip to content

Instantly share code, notes, and snippets.

@emhoracek
Last active August 29, 2015 14:27
Show Gist options
  • Save emhoracek/cb6d5179e746774770ab to your computer and use it in GitHub Desktop.
Save emhoracek/cb6d5179e746774770ab to your computer and use it in GitHub Desktop.
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Either
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead xs = Just $ head xs
maybeTHead :: MaybeT IO Char
maybeTHead = MaybeT $ do
str <- getUserInput
let head1 = safeHead str
return head1
getUserInput = getLine
returnHeads :: IO (Maybe Char)
returnHeads = do
heads <- runMaybeT $ maybeTHead
return heads
maybeHead :: [a] -> Maybe (Either String a)
maybeHead list = do
runEitherT $ eitherSafeHead list
safeHead []
eitherSafeHead :: [a] -> EitherT String Maybe a
eitherSafeHead list = EitherT $ do
let result = safeHead list
case result of
Nothing -> return $ Left "empty!!"
Just x -> return $ Right x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment