import Control.Monad.Reader | |
hello :: Reader String String | |
hello = do | |
name <- ask | |
return ("hello, " ++ name ++ "!") | |
bye :: Reader String String | |
bye = do | |
name <- ask | |
return ("bye, " ++ name ++ "!") | |
convo :: Reader String String | |
convo = do | |
c1 <- hello | |
c2 <- bye | |
return $ c1 ++ c2 | |
main = print . runReader convo $ "adit" |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
tabatkins
commented
Aug 7, 2015
Out of curiosity, is there any reason not to write the last line as the (imo simpler to me as a relative Haskell newb) as: main = print $ runReader convo "adit" ? I ask because Similarly, line 17 uses |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
nbenns
Nov 9, 2016
ok I see that shared data, but the readers are literally being unwrapped concatenated and then rewrapped... what about <$> and >>=?
nbenns
commented
Nov 9, 2016
ok I see that shared data, but the readers are literally being unwrapped concatenated and then rewrapped... what about <$> and >>=? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
nbenns
Nov 9, 2016
answered my own question:
convo = hello >>= \h -> (bye >>= \b -> return $ h ++ b)
so the reader makes sure hello
bye
and the return
ed functions are all called with the same input
alternatively with fmap (<$>):
convo = hello >>= \h -> (\b -> h ++ b) <$> bye
or with apply (<*>)
import Control.Monad.Reader
hello :: Reader String String
hello = asks $ \name -> ("hello, " ++ name ++ "!")
bye :: Reader String String
bye = asks $ \name -> ("bye, " ++ name ++ "!")
convo :: Reader String String
convo = asks (const (++)) <*> hello <*> bye
main = print . runReader convo $ "adit"
nbenns
commented
Nov 9, 2016
•
edited
edited
answered my own question: convo = hello >>= \h -> (bye >>= \b -> return $ h ++ b) so the reader makes sure alternatively with fmap (<$>): convo = hello >>= \h -> (\b -> h ++ b) <$> bye or with apply (<*>) import Control.Monad.Reader
hello :: Reader String String
hello = asks $ \name -> ("hello, " ++ name ++ "!")
bye :: Reader String String
bye = asks $ \name -> ("bye, " ++ name ++ "!")
convo :: Reader String String
convo = asks (const (++)) <*> hello <*> bye
main = print . runReader convo $ "adit" |
Out of curiosity, is there any reason not to write the last line as the (imo simpler to me as a relative Haskell newb) as:
?
I ask because
$
is typically only used to allow the evaluation of its right hand side, when you need to call more functions to produce the argument you want to pass. Using it with a literal RHS because you needed to do some work to produce the function was confusing to me at first.Similarly, line 17 uses
$
while lines 6 and 11 use parens. That's a more typical usage of$
, so I grasped it quickly enough, but the style difference for no apparent reason feels like bad form for example code. Using one or the other for all three would be nice. ^_^