[Haskell-beginners] Combining IO and Either function to "EitherT e IO a"

Mateusz Kowalczyk fuuzetsu at fuuzetsu.co.uk
Wed Mar 5 14:31:59 UTC 2014


On 05/03/14 13:53, Nathan Hüsken wrote:
> Hey,
> 
> I have a function
> 
> |func1 :: IO String
> |
> 
> and another:
> 
> |func2 :: String -> Either String String
> |
> 
> and I want to combine them, giving the output of the first as the input
> as the second.
> 
> |func3 :: IO (Either String String)
> func3 = do
>   tmp <- func1
>   return (func2 tmp)
> |
> 

This is just ‘fmap func2 func1’ or using the operator, ‘func2 <$> func1’.

> Ok, possible. But I rather would like a result of type "EitherT String
> IO String".
> So how can I combine these function in a smart way, to get the needed
> result?

After using ‘func2 <$> func1’ we have ‘IO (Either String String)’ as you
point out. The ‘EitherT’ constructor has type ‘m (Either e a)’. Here if
m = IO, e = String, a = String so we have exactly what we need:

> > :t EitherT $ func2 <$> func1
> EitherT $ func2 <$> func1 :: EitherT String IO String

> 
> Thanks!
> Nathan
> 
> 
> 
> 
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
> 


-- 
Mateusz K.


More information about the Beginners mailing list