Skip to content

Instantly share code, notes, and snippets.

@deque-blog

deque-blog/passwordIO.idr

Last active Nov 9, 2017
Embed
What would you like to do?
password : Nat -> (String -> Bool) -> IO Bool
password maxAttempt valid = recur 1 where
recur n = do
putStrLn "Password:" -- Ask the user for a password
attempt <- getLine -- Read the password entered by the user
if valid attempt -- In case of valid password:
then do
putStrLn ("Successful login after " ++ show n ++ " attempt(s).")
pure True
else do -- In case of invalid passord:
putStrLn ("Login failed: " ++ show n ++ " attempt(s).")
if n < maxAttempt
then recur (n + 1) -- * Try again with one less attempt
else pure False -- * Stop after reaching maximal number of attempt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment