将字符串列表解析为Int

时间:2019-03-19 19:32:20

标签: haskell

我正在从文件中读取职位并获取此信息:

["[6,5,3]","[4,1,4]","[3,7,6]"]

并且我需要一个类似Int的列表:

[[6,5,3],[4,1,4,],[3,7,6]]

这是源代码:

import System.IO
import qualified Data.Text    as Text
import qualified Data.Text.IO as Text

main = do
    ls <- fmap Text.lines (Text.readFile "file.txt")

1 个答案:

答案 0 :(得分:0)

只需使用read

l = ["[6,5,3]","[4,1,4]","[3,7,6]"]
[read x :: [Int] | x <- l]
=> [[6,5,3],[4,1,4],[3,7,6]]

如果["[6,5,3]","[4,1,4]","[3,7,6]"]行是一个完整的字符串,则可以全部读取:

read line :: [[Int]]