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

15 lines
391 B
Haskell
Raw Normal View History

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