Skip to content

Instantly share code, notes, and snippets.

@dustinlacewell
Last active September 15, 2021 04:42
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 dustinlacewell/f61df03ffeb41976ea16193281978f09 to your computer and use it in GitHub Desktop.
Save dustinlacewell/f61df03ffeb41976ea16193281978f09 to your computer and use it in GitHub Desktop.
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