gpt4 book ai didi

haskell - Parsec 不解析换行符

转载 作者:行者123 更新时间:2023-12-02 21:29:30 25 4
gpt4 key购买 nike

我有以下代码:

import Text.ParserCombinators.Parsec
import Control.Applicative hiding ((<|>))
import Control.Monad

data Test = Test Integer Integer deriving Show

integer :: Parser Integer
integer = rd <$> many1 digit
where rd = read :: String -> Integer

testParser :: Parser Test
testParser = do
a <- integer
char ','
b <- integer
eol
return $ Test a b

eol :: Parser Char
eol = char '\n'

main = forever $ do putStrLn "Enter the value you need to parse: "
input <- getLine
parseTest testParser input

但是当我实际尝试在 ghci 中解析我的值时,它不起作用。

ghci > main
Enter the value you need to parse:
34,343\n
parse error at (line 1, column 7):
unexpected "\\"
expecting digit or "\n"

关于我在这里遗漏的任何想法?

最佳答案

问题似乎是您期待换行符,但您的文本不包含换行符。将 eol 更改为

eol :: Parser ()
eol = void (char '\n') <|> eof

它会起作用。

关于haskell - Parsec 不解析换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22670492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com