her.esy.fun/src/posts/0010-Haskell-Now/io_sum_safe.hs
Yann Esposito (Yogsototh) cc7ee03907
wip
2019-12-25 22:17:22 +01:00

19 lines
534 B
Haskell

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."