-
-
Save dustinlacewell/49f4f682f91465e055c691c2778c9501 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 | |
import Control.Monad | |
readEither' :: Read a => String -> String -> Either String a | |
readEither' e i = | |
case readEither i of | |
Left x -> Left e | |
Right y -> Right y | |
asInt :: String -> Either String Integer | |
asInt = readEither' "You must enter a number." | |
atLeast2 :: (Ord b, Num b) => b -> Either [Char] b | |
atLeast2 v | |
| v <= 2 = Left "The value must be greater than 2." | |
| otherwise = Right v | |
getInput :: String -> (String -> Either String a) -> IO a | |
getInput prompt check = do | |
putStr prompt | |
input <- getLine | |
case check input of | |
Left msg -> do | |
print msg | |
getInput prompt check | |
Right v -> return 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