Created
April 27, 2015 11:59
-
-
Save orionll/723161bab1a32f0ae248 to your computer and use it in GitHub Desktop.
Calculate string length with the Reader monad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import qualified Control.Monad.Trans.Reader as R | |
| strLength :: R.Reader String Int | |
| strLength = do | |
| s <- R.ask | |
| case s of | |
| [] -> return 0 | |
| (_:cs) -> fmap (+1) (R.local (const cs) strLength) | |
| main = do | |
| print (R.runReader strLength "abc") -- 3 | |
| print (R.runReader strLength "abcde") -- 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment