Argument arguments parse comand

I am trying to parse the command line arguments in haskell.

The following is sample code:

import System.Environment

work :: [Integer] -> Int
work (s:r:t:es) = length es

main :: IO ()
main = getArgs >>= putStrLn . show . work . (map read)

I execute it with:

./test 2 10 10 [7, 3, 5, 4, 4]

The result is 5 as expected. But if I replace the length with the sum and Int with Integer, execution causes an error

test: Prelude.read: no parse

Can someone explain how to do this?

+4
source share
1 answer

The list returned getArgswould look like this: ["2", "10", "10", "[7,", "3,", "5,", "4,", "4]"]. The first three of these lines are valid string representations of integers, while the others are not. Therefore, when you use readon them, you will receive an error message.

, , , length , read .

, , , , . , .

, , , , read.

, ( "[7, 3, 5, 4, 4]" ), read [Integer].

+6

Source: https://habr.com/ru/post/1675443/


All Articles