Skip to content

Instantly share code, notes, and snippets.

@yuanw
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save yuanw/89c07457750cdf12dee9 to your computer and use it in GitHub Desktop.

Select an option

Save yuanw/89c07457750cdf12dee9 to your computer and use it in GitHub Desktop.
Haskell Monad Tranformation
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