Skip to content

Instantly share code, notes, and snippets.

@egonSchiele
Created June 10, 2013 20:51
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save egonSchiele/5752172 to your computer and use it in GitHub Desktop.
Save egonSchiele/5752172 to your computer and use it in GitHub Desktop.
Reader monad example
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"
@nbenns
Copy link

nbenns commented Nov 9, 2016

answered my own question:

convo = hello >>= \h -> (bye >>= \b -> return $ h ++ b)

so the reader makes sure hello bye and the returned 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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment