her.esy.fun/src/posts/0010-Haskell-Now/io_sum_safe.hs

19 lines
534 B
Haskell
Raw Normal View History

2019-12-25 21:17:22 +00:00
import Data.Maybe
maybeRead :: Read a => String -> Maybe a
maybeRead s = case reads s of
[(x,"")] -> Just x
_ -> Nothing
getListFromString :: String -> Maybe [Integer]
getListFromString str = maybeRead $ "[" ++ str ++ "]"
main :: IO ()
main = do
putStrLn "Enter a list of numbers (separated by comma):"
input <- getLine
let maybeList = getListFromString input in
case maybeList of
Just l -> print (sum l)
Nothing -> putStrLn "Bad format. Good Bye."