Haskell string to list
["6","","[[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18],[19,20,21,22,23,24],[25,26,27,28,29,30],[31,32,33,34,35,36]]"] I must get from this 6 and [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18],[19,20,21,22,23,24],[25,26,27,28,29,30],[31,32,33,34,35,36]] I read from file in file it look
6. [[1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16,17,18],[19,20,21,22,23,2开发者_运维技巧4],[25,26,27,28,29,30],[31,32,33,34,35,36]].
I tried with map and read to convert string but it wasnt working. sry for english
The problem with using map
and read
directly is, that your list elements don't all have the same types. The first element is (or better: should be converted to) an Int
, the second an empty list and the third an `[[Int]]
.
To convert the first element of the list into an Int
, you can say something like read $ head xs :: Int
, where xs
is your list of strings.
The second element cannot be directly converted by read, since an empty string will result in an exception (Prelude.read: no parse).
To convert the third element, which is a list of lists of integers, you can simply say something like read $ xs !! 2 :: [[Int]]
.
This is not safe or elegant, but if your input always has this structure it should work.
精彩评论