Skip to content

Instantly share code, notes, and snippets.

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