Haskell 和 Parsec:解析两个独立的数字列表

问题描述 投票:0回答:1

我有生以来第一次尝试秒差距,我发现一项任务异常困难。

我想解析两个由

|
字符分隔的数字列表。 这是一个例子:

Data 1: 43 76 123 98 32 | 32 88 43 123 43

这是我到目前为止找到的代码

data Data = Data Int [Int] [Int]
    deriving Show

toInt :: String -> Int
toInt = read

parseRow :: Parser Data
parseRow = do
    _ <- Parsec.string "Data"
    _ <- Parsec.spaces
    cid <- toInt <$> many1 Parsec.digit
    firstList <- map toInt <$> between (Parsec.string ": ") (Parsec.char '|') (many1 (many1 Parsec.digit <* Parsec.space))
    secondList <- map toInt <$> sepBy1 (many1 Parsec.digit) (Parsec.char ' ')
    return $ Data cid firstList secondList

解析时会感到困惑

firstList
。我想我搞砸了解析数字之间的空格,但看不到明显的错误。

展望未来,最适合初学者的秒差距介绍是什么?我找到了一些教程,但很高兴听到建议。

parsing haskell parsec
1个回答
0
投票

Parsec.char '|'
中的
Parsec.string "| "
替换为
firstList
。否则,
secondList
必须处理输入开头的额外空格,这是它不希望的。

© www.soinside.com 2019 - 2024. All rights reserved.