i need help, i have to read a list like this ["1", "2", "3"] and make a list of integer of it [1,2,3] so i use read.
the problem is, when the list looks like ["1", "2", "a"] the programm quits because of the error that there is a char in it.
how to check or throw an error to prevent this error?
You should be using reads, not read.
Prelude> :m Data.Maybe
Prelude Data.Maybe> (map (fmap fst . listToMaybe . reads) ["1", "2", "3"]) :: [Maybe Integer]
[Just 1,Just 2,Just 3]
Prelude Data.Maybe> (map (fmap fst . listToMaybe . reads) ["1", "2", "a"]) :: [Maybe Integer]
[Just 1,Just 2,Nothing]
Prelude Data.Maybe>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With