-
-
Save dustinlacewell/f61df03ffeb41976ea16193281978f09 to your computer and use it in GitHub Desktop.
This file contains 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 Text.Read | |
asInt v = readEither "You must enter a number." :: Either String Int | |
atLeast2 v | |
| v <= 2 = Left "The value must be greater than 2." | |
| otherwise = Right v | |
checkVal [] v = Right v | |
checkVal (p:ps) v = | |
p v >>= checkVal ps | |
getInput :: String -> [String -> Either String a] -> IO (Either b String) | |
getInput p cs = do | |
putStr p | |
e <- checkVal cs <$> getLine | |
case e of | |
Left msg -> getInput p cs | |
Right v -> return $ Right v | |
main = do | |
a <- getInput "a: " [asInt] | |
b <- getInput "b: " [asInt] | |
c <- getInput "c: " [asInt] | |
n <- getInput "n: " [asInt, atLeast2] | |
print $ (a ^ n) + (b ^ n) == (c ^ n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment