![](/img/trans.png)
[英]Haskell: How to use attoparsec in order to read a nested list from a ByteString
[英]Haskell parse list of integers with Attoparsec
當整數列表可以是任意長度時,我需要一些幫助來解析文件中逗號分隔的整數列表。 該字符串可能看起來像“ 1,2,3,4,5,6,7,8,...”,而我需要列出的列表將像[1,2,3,4,5,6,7, 8,...]。
文件格式如下:
0,0:1; -- minimum of 1 integer after the :
0,1:1,2;
0,2:5;
0,3:5,16,223,281; -- any amount of integers can follow the :
...
我的解析器當前僅讀取一個整數,但是它需要開始讀取更多整數。 我可以使用takeTill將所有數字讀入ByteString,但是隨后我必須解析另一個字符串,其問題是不知道到底可以有多少個數字:
parseTile :: Parser Tile
parseTile = do
x <- decimal
char ','
y <- decimal
char ':'
--t <- takeTill (\x -> x == ';')
t <- decimal
char ';'
return $ Tile x y t
我找到了 ,但這對我沒有幫助,因為我的文件不是json文件。
您可以使用sepBy
和decimal
:
parseTile :: Parser Tile
parseTile = do
x <- decimal
char ','
y <- decimal
char ':'
t <- decimal `sepBy` (char ',')
char ';'
return $ Tile x y t
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.