Convert a String list to an integer list list in haskell

0

I have this list

["0 1 1 0","1 0 1 0","1 1 0 1","0 0 1 0"]

and I want to convert every position of that list into a list of integers ie

[[0110],[1010],[1101],[0010]]
    
asked by Efrainrodc 17.06.2017 в 20:37
source

1 answer

1
intmap :: [String] -> [[Int]]
intmap x = map (\l -> map read (words l)) x

Shelling the code: intmap receives a list of Strings and returns a list of integer lists. the lambda function \l -> map read (words l) accepts a String and converts it to a list of elements separated by spaces, and then converts each element to an integer. This function is applied to each item in the list with map .

The function can fail if the strings do not represent an integer. To expand it, exceptions or the Maybe monad could be used.

    
answered by 17.06.2017 / 21:28
source