English 中文(简体)
我怎么才能让普鲁采奇让我叫 " 阅读 "?
原标题:How do I get Parsec to let me call `read` :: Int?

我得到了以下的, 什么样的类型检查:

p_int = liftA read (many (char    ) *> many1 digit <* many (char    ))

现在,正如函数名称所暗示的,我希望它能给我一个中间点。 但如果我这样做:

p_int = liftA read (many (char    ) *> many1 digit <* many (char    )) :: Int

我得到这个类型错误 :

Couldn t match expected type `Int  with actual type `f0 b0 
In the return type of a call of `liftA 
In the expression:
    liftA read (many (char    ) *> many1 digit <* many (char    )) ::
      Int
In an equation for `p_int :
    p_int
      = liftA read (many (char    ) *> many1 digit <* many (char    )) ::
          Int

是否有一种更简单、更清洁的方法来分析整数,而整数可能有白空格?还是一种方法来解决这个问题?

归根结底,我希望这成为以下内容的一部分:

betaLine = string "BETA " *> p_int <*> p_int  <*> p_int <*>
           p_int <*> p_parallel <*> p_exposure <* eol

用来分析看起来像这样的线条 :

BETA  6 11 5 24 -1 oiiio

因此,我最终可以叫一个BetaPair 建构器,它需要这些价值(有些是Int,有些是其他类型,如[勘探]和平行)

(如果你好奇的话, 这是用于文件格式的解析器, 除其他外, 代表有氢的乙型树脂配对的蛋白质。 我无法控制文件格式! )

最佳回答

p_int 是生成 Int 的解析器, 所以类型将是 Parker Int 或类似的1 。

p_int = liftA read (many (char    ) *> many1 digit <* many (char    )) :: Parser Int

或者,您可以输入 read 函数, (阅读 * 字符串 - & gt; Int) 来告诉编纂者该表达式的类型。

p_int = liftA (read :: String -> Int) (many (char    ) *> many1 digit <* many (char    )) :: Int

至于更清洁的方法,请考虑以 spaces 取代 many(char)

1 ParsecT x y z Iint ,例如。

问题回答

我该如何让小分析让我给 read * Int 打电话?

第二个答案是"不要读"

使用 read 等同于您已经解析的重新分类数据 - 所以在剖析剖析器中使用它是一种代码气味。 解析自然数字是无害的, 但 read 的语义与 prassic 不同, 它适合哈斯凯尔的词汇语法, 所以使用它来使用更复杂的数字格式有问题 。

如果您不想去定义 LanguageDef , 并且使用 prassic s Token 模块来定义 , 这里是一个不使用阅读的自然数字解析器 :

-- | Needs @foldl @ from Data.List and 
-- @digitToInt@ from Data.Char.
--
positiveNatural :: Stream s m Char => ParsecT s u m Int
positiveNatural = 
    foldl  (a i -> a * 10 + digitToInt i) 0 <$> many1 digit

你可能会发现

Text-Megaparsec-Lexer.integer :: MonadParsec s m Char => m Integer

做你想做的事 做你想做的事

香草分析图书馆似乎缺少了许多明显的分析师,这导致了“包括电池的”分析衍生物包的崛起。 我想,分析维护者最终会变得更好。

https://hackage.haskell.org/package/megaparsec-42.0/docs/Text-Megaparsec-Lexer.html

最新更新

或用香草剖析法:

Prelude Text.Parsec Text.Parsec.Language Text.Parsec.Token> parse ( integer . makeTokenParser $ haskellStyle ) "integer" "-1234"
Right (-1234)




相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?