Last active
August 29, 2015 14:11
-
-
Save yuanw/89c07457750cdf12dee9 to your computer and use it in GitHub Desktop.
Haskell Monad Tranformation
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 Data.Maybe | |
| import Control.Monad | |
| import Control.Monad (guard) | |
| import Control.Monad.Trans (lift) | |
| import Control.Monad.Trans.Maybe | |
| import Data.Char | |
| getValidPassphrase :: MaybeT IO String | |
| getValidPassphrase = do s <- lift getLine | |
| guard (isValid s) -- MonadPlus provides guard. | |
| return s | |
| -- The validation test could be anything we want it to be. | |
| isValid :: String -> Bool | |
| isValid s = length s >= 8 | |
| && any isAlpha s | |
| && any isNumber s | |
| && any isPunctuation s | |
| askPassword :: MaybeT IO () | |
| askPassword = do lift $ putStrLn "Insert your new password:" | |
| value <- msum $ repeat getValidPassphrase | |
| lift $ putStrLn "Storing in database..." | |
| main :: IO () | |
| main = do | |
| result <- runMaybeT askPassword | |
| if isNothing result | |
| then main | |
| else putStrLn "Bye!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment