简体   繁体   English

要列出的Haskell字符串

[英]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,24],[25,26,27,28,29,30],[31,32,33,34,35,36]]“我必须从这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]]我从文件中读取文件看起来

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]]. 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 tried with map and read to convert string but it wasnt working. 我尝试使用map并读取转换字符串,但它无法正常工作。 sry for english sry for english

The problem with using map and read directly is, that your list elements don't all have the same types. 直接使用mapread的问题是,list元素并不都具有相同的类型。 The first element is (or better: should be converted to) an Int , the second an empty list and the third an `[[Int]] . 第一个元素是(或者更好:应该转换为)一个Int ,第二个元素是空列表,第三个元素是`[[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. 要将列表的第一个元素转换为Int ,您可以说类似于read $ head xs :: Int ,其中xs是您的字符串列表。

The second element cannot be directly converted by read, since an empty string will result in an exception (Prelude.read: no parse). 第二个元素不能通过read直接转换,因为空字符串将导致异常(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]] 要转换第三个元素,这是一个整数列表的列表,你可以简单地说像read $ xs !! 2 :: [[Int]] read $ xs !! 2 :: [[Int]] . read $ xs !! 2 :: [[Int]]

This is not safe or elegant, but if your input always has this structure it should work. 这不安全或优雅,但如果您的输入总是具有这种结构,它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM