There is a list of String [“f”, “1”, “h”, “6”, “b”, “7”].
How can I count Int in this list?
Now I have this algorithm but it’s not so good.
import Data.Char
let listOfStrings = ["f", "1", "h", "6", "b", "7"]
let convertedString = "f1h6b7"
let listOfInt = map (\x -> read [x]::Int) (filter (\x -> isDigit x) convertedString)
length listOfInt
Prelude> 3
Besides, I can’t convert listOfStrings to one string. This algorithm doesn’t even work properly
Can you help me with optimization?
1) Use
reads :: Reads Int(this expression is justreads :: String -> [(Int, String)]in disguise) to test whether a string is a representation of an integer value:Why
reads? Because it returns additional information about parsing process from which we can conclude if it was successful.read :: Intwould just throw an exception.2) then filter a list of strings with it and take its length: