Files
haskell-math/app/Utils.hs

26 lines
664 B
Haskell

module Utils (askNumber, askChoice) where
import Text.Read (readMaybe)
import System.Exit (exitSuccess)
-- Ask user for an integer > 1, or exit on invalid input
askNumber :: String -> IO Integer
askNumber s = do
putStrLn s
input <- getLine
case readMaybe input of
Just n | n > 1 -> return n
_ -> do
putStrLn "Not a valid integer"
exitSuccess
askChoice :: Int -> IO Int
askChoice maxChoice = do
putStrLn "Enter your choice:"
input <- getLine
case readMaybe input of
Just n | n >= 1 && n <= maxChoice -> return n
_ -> do
putStrLn "Invalid choice"
exitSuccess