Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.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
Haskell 绑定分析器返回错误的类型_Haskell - Fatal编程技术网

Haskell 绑定分析器返回错误的类型

Haskell 绑定分析器返回错误的类型,haskell,Haskell,我正在读haskell的书,很好奇为什么bind操作符的返回类型对我来说很奇怪 对于给定的定义 type Parser a = String -> [(a, String)] item :: Parser Char item = \inp -> case inp of [] -> [] (x:xs) -> [(x,xs)] bind :: Parser a -> (a -> Pa

我正在读haskell的书,很好奇为什么bind操作符的返回类型对我来说很奇怪

对于给定的定义

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

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

bind :: Parser a -> (a -> Parser b) -> Parser b
p `bind` f = \inp -> concat [ f x inp' | (x, inp') <- p inp]
返回类型为

>> :t z
z :: Parser ([Char], Char)
问题:
(1) 不应该返回(Char,[Char])的类型吗?查看列表理解,“(x,in p))p>我不确定什么是<代码>结果<代码>,但这是一个关联性的问题。请考虑您的代码> z <代码>:

let z = item `bind` (\x -> (\y -> result (x,y))) "Rohit"
这相当于

let z = item `bind` ((\x -> (\y -> result (x,y))) "Rohit")
      = item `bind` (\y -> result ("Rohit",y))
我相信通过添加以下括号,您将获得您想要的结果:

let z = (item `bind` (\x -> (\y -> result (x,y)))) "Rohit"

我不确定什么是<代码>结果<代码>,但这是一个关联性的问题。请考虑你的代码> z >:

let z = item `bind` (\x -> (\y -> result (x,y))) "Rohit"
这相当于

let z = item `bind` ((\x -> (\y -> result (x,y))) "Rohit")
      = item `bind` (\y -> result ("Rohit",y))
我相信通过添加以下括号,您将获得您想要的结果:

let z = (item `bind` (\x -> (\y -> result (x,y)))) "Rohit"

假设
result
的类型为
a->[a]
(您是指列表单子的
return
),您遇到的问题来自使用中缀
bind

item `bind` (\x -> (\y -> result (x,y))) "Rohit"
被解析为

bind item ((\ x y -> result (x, y)) "Rohit")
而不是你所期望的,我假设是:

bind item (\ x y -> result (x, y)) "Rohit"
您可以使用
$
解决此问题:

let z = item `bind` (\x -> (\y -> result (x,y))) $ "Rohit"

假设
result
的类型为
a->[a]
(您是指列表单子的
return
),您遇到的问题来自使用中缀
bind

item `bind` (\x -> (\y -> result (x,y))) "Rohit"
被解析为

bind item ((\ x y -> result (x, y)) "Rohit")
而不是你所期望的,我假设是:

bind item (\ x y -> result (x, y)) "Rohit"
您可以使用
$
解决此问题:

let z = item `bind` (\x -> (\y -> result (x,y))) $ "Rohit"

非常感谢,我会记住的☺我按照建议进行了尝试,但得到了以下异常*Main>let z=item
bind
(\x->(\y->result(x,y))$“Rohit”:89:35:无法将type
String->[((Char,String),String)]与
[(b0,String)]匹配,预期类型:[(b0,String)]实际类型:解析器(Char String)在调用
result'可能原因:
result'的返回类型中,(\y->result(x,y))中的表达式:result(x,y)中的参数太少,
result
的类型是什么?您的代码段中没有定义它。非常感谢,我会记住这一点☺我按照建议进行了尝试,但得到了以下异常*Main>let z=item
bind
(\x->(\y->result(x,y))$“Rohit”:89:35:无法将type
String->[((Char,String),String)]与
[(b0,String)]匹配,预期类型:[(b0,String)]实际类型:解析器(Char String)在调用
result'可能原因:
result'的返回类型中,表达式(\y->result(x,y))中的表达式:result(x,y)中的参数太少(\y->result(x,y))
result的类型是什么?
?它没有在代码段中定义。Main>让z=(item
bind
(\x->(\y->result(x,y)))“Rohit“:91:36:无法将类型
String->[((Char,String),String)]与
[(b0,String)]匹配预期类型:[(b0,String)]实际类型:
result'可能原因:
result'调用的返回类型中的解析器(Char,String)应用于表达式:result中的参数太少。”(x,y)在表达式中:(\y->result(x,y))Main>let z=(item
bind
(\x->(\y->result(x,y)))“Rohit”:91:36:无法将type
String->[((Char,String),String)]与
[(b0,String)]预期类型匹配:[(b0,String)]实际类型:解析器(Char String)在调用的返回类型中,
result'可能原因:
result'应用于表达式中的参数太少:(\y->result(x,y))