Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Parsing 解析器的问题_Parsing_Haskell - Fatal编程技术网

Parsing 解析器的问题

Parsing 解析器的问题,parsing,haskell,Parsing,Haskell,我希望有人能帮助我理解下面的代码 type Parser a = String -> [(a,String)] item :: Parser Char item = \ s -> case s of [] -> [] (x:xs) -> [(x,xs)] returnP :: Parser a returnP a = \s -> [(a,s)] (>>=) :: Parser a -> (a -> Parser b

我希望有人能帮助我理解下面的代码

type Parser a = String -> [(a,String)]

item :: Parser Char
item = \ s -> case s of
    [] -> []
    (x:xs) -> [(x,xs)]

returnP :: Parser a
returnP a  = \s -> [(a,s)] 


(>>=) :: Parser a -> (a -> Parser b) -> Parser b
p>>=f = \s -> case p s of
    [(x,xs)]-> f x xs
    _ -> []


twochars :: Parser (Char,Char)
twochars= item >>= \a -> item >>= \b -> returnP (a,b)

一切似乎都很清楚,但我不明白twochars函数最后一行的lampda函数。如果有人能给我一个解释就好了。

为了清晰起见重写
twochars
函数,它基本上是:

twochars = 
  item >>= \a -> -- parse a character and call it `a`
  item >>= \b -> -- parse another character and call it `b`
  returnP (a,b)  -- return the tuple of `a` and `b`
这里的lambda只是为解析后的字符引入名称,并将它们传递给后面的计算部分

它们对应于您定义的绑定中的第二个参数:

(>>=) :: Parser a        -- your item
      -> (a -> Parser b) -- your lambda returning another parse result
      -> Parser b        

你能说在什么程度上事情是不清楚的吗?你懂语法吗?优先顺序混乱吗?你知道变量的作用域是怎样的吗?你对这种行为感到困惑吗?
twochars
是否暴露了
(>>=)
实现中的错误?简言之:你想解释什么?我忘了告诉你前奏(>>=)藏在哪里了。我想解释一下\a和\b以及为什么这样做。一切都很清楚,但我无法抵抗最后一行