Reader and ReaderT

Hi, I have the following program: import Control.Monad.Reader first :: Reader [String] String first = do strings <- ask return $ if (null strings) then "empty" else head strings printFirst :: ReaderT [String] IO () printFirst = do strings <- ask let theFirstString = runReader first strings liftIO $ putStrLn theFirstString main = runReaderT printFirst ["first","second"] It compiles and works. However, in the printFirst function I am explicitly using ask and I am calling runReader. Can I somehow avoid doing that? The following version of the printFirst function does not compile. printFirst :: ReaderT [String] IO () printFirst = do theFirstString <- first liftIO $ putStrLn theFirstString Program2.hs:11:21: Couldn't match type `Data.Functor.Identity.Identity' with `IO' Expected type: ReaderT [String] IO String Actual type: Reader [String] String In a stmt of a 'do' block: theFirstString <- first In the expression: do { theFirstString <- first; liftIO $ putStrLn theFirstString } Can I somehow call first without using ask and runReader ? Regards Grzegorz Balcerek

Hi,
You could generalize first to ReaderT [String] a String, so that you
could use it as ReaderT [String] IO String. You don't need to change
the implementation - just change the type.
Hope this helps,
Alexey
On Fri, Oct 16, 2015 at 12:37 AM, Grzegorz Balcerek
Hi,
I have the following program:
import Control.Monad.Reader
first :: Reader [String] String first = do strings <- ask return $ if (null strings) then "empty" else head strings
printFirst :: ReaderT [String] IO () printFirst = do strings <- ask let theFirstString = runReader first strings liftIO $ putStrLn theFirstString
main = runReaderT printFirst ["first","second"]
It compiles and works. However, in the printFirst function I am explicitly using ask and I am calling runReader. Can I somehow avoid doing that? The following version of the printFirst function does not compile.
printFirst :: ReaderT [String] IO () printFirst = do theFirstString <- first liftIO $ putStrLn theFirstString
Program2.hs:11:21: Couldn't match type `Data.Functor.Identity.Identity' with `IO' Expected type: ReaderT [String] IO String Actual type: Reader [String] String In a stmt of a 'do' block: theFirstString <- first In the expression: do { theFirstString <- first; liftIO $ putStrLn theFirstString }
Can I somehow call first without using ask and runReader ?
Regards Grzegorz Balcerek
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Thank you! This works: first :: (Monad a) => ReaderT [String] a String Grzegorz W dniu 2015-10-15 o 23:55, Alexey Shmalko pisze:
Hi,
You could generalize first to ReaderT [String] a String, so that you could use it as ReaderT [String] IO String. You don't need to change the implementation - just change the type.
Hope this helps, Alexey
On Fri, Oct 16, 2015 at 12:37 AM, Grzegorz Balcerek
wrote: Hi,
I have the following program:
import Control.Monad.Reader
first :: Reader [String] String first = do strings <- ask return $ if (null strings) then "empty" else head strings
printFirst :: ReaderT [String] IO () printFirst = do strings <- ask let theFirstString = runReader first strings liftIO $ putStrLn theFirstString
main = runReaderT printFirst ["first","second"]
It compiles and works. However, in the printFirst function I am explicitly using ask and I am calling runReader. Can I somehow avoid doing that? The following version of the printFirst function does not compile.
printFirst :: ReaderT [String] IO () printFirst = do theFirstString <- first liftIO $ putStrLn theFirstString
Program2.hs:11:21: Couldn't match type `Data.Functor.Identity.Identity' with `IO' Expected type: ReaderT [String] IO String Actual type: Reader [String] String In a stmt of a 'do' block: theFirstString <- first In the expression: do { theFirstString <- first; liftIO $ putStrLn theFirstString }
Can I somehow call first without using ask and runReader ?
Regards Grzegorz Balcerek
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (2)
-
Alexey Shmalko
-
Grzegorz Balcerek