Skip to content

Instantly share code, notes, and snippets.

@mikevdg

mikevdg/Main.hs Secret

Created January 24, 2021 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikevdg/aa9b5505b528f0836b85a716667e51a0 to your computer and use it in GitHub Desktop.
Save mikevdg/aa9b5505b528f0836b85a716667e51a0 to your computer and use it in GitHub Desktop.
module Main where
import Data.Char ( isLower, isUpper )
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Reader
import Control.Monad.Trans.Class
type Env = (Maybe String, Maybe String, Maybe String)
main :: IO ()
main = do
maybeCreds <- runMaybeT $ do
usr <- runReaderT readUserName (Nothing, Nothing, Nothing)
email <- readEmail
pass <- readPassword
return (usr, email, pass)
case maybeCreds of
Nothing -> print "Could not log in."
Just (u, e, p) -> print $ "Logging in: "++u++e++p
readUserName :: MaybeT (ReaderT Env IO) String
readUserName = MaybeT $ do
(maybeOldUser, _, _ ) <- ask
case maybeOldUser of
Just str -> return $ Just str
Nothing -> do
lift $ putStrLn "Please enter your username: "
input <- lift getLine
if length input > 5
then return $ Just input
else return Nothing
readEmail :: MaybeT IO String
readEmail = MaybeT $ do
putStrLn "Please enter your email: "
str <- getLine
if '@' `elem` str && '.' `elem` str
then return $ Just str
else return Nothing
readPassword :: MaybeT IO String
readPassword = MaybeT $ do
putStrLn "Please enter your password: "
str <- getLine
if length str < 8 || not (any isUpper str) || not (any isLower str)
then return Nothing
else return $ Just str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment