Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Reader and ReaderT (Grzegorz Balcerek)
2. Re: Reader and ReaderT (Alexey Shmalko)
3. Re: Reader and ReaderT (Grzegorz Balcerek)
----------------------------------------------------------------------
Message: 1
Date: Thu, 15 Oct 2015 23:37:14 +0200
From: Grzegorz Balcerek <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Reader and ReaderT
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8; format=flowed
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
------------------------------
Message: 2
Date: Fri, 16 Oct 2015 00:55:01 +0300
From: Alexey Shmalko <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Reader and ReaderT
Message-ID:
<cafc2pc7tydmthn4anrjbxr22wlua9-u6rgxlzzqvoyv2cdq...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
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
<[email protected]> 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
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
Message: 3
Date: Fri, 16 Oct 2015 12:46:27 +0200
From: Grzegorz Balcerek <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Reader and ReaderT
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252; format=flowed
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
> <[email protected]> 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
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 88, Issue 12
*****************************************