简体   繁体   English

Haskell:使用翻转会导致错误:'.>' 的方程具有不同数量的参数

[英]Haskell: Usage of flip results in error: Equations for ‘.>’ have different numbers of arguments

I am struggling to understand what's going on here.我正在努力理解这里发生了什么。 I want to implement a data type Direction and define a commutative operator .> for it.我想实现一个数据类型Direction并为其定义一个交换运算符.> So far I have this:到目前为止,我有这个:

data Direction = N | E | S | W | None

(.>) :: Direction -> Direction -> [Direction]
N .> S = [None]
W .> E = [None]
(.>) = flip (.>)

I get the error Equations for '.>' have different numbers of arguments .我收到错误Equations for '.>' have different numbers of arguments That's what I don't understand, because both sides of the equation have the same numers of arguments when checked in ghci:这就是我不明白的,因为在 ghci 中检查时,等式的两边都有相同数量的参数:

λ> :t (.>)
(.>) :: Direction -> Direction -> [Direction]
λ> :t flip (.>)
flip (.>) :: Direction -> Direction -> [Direction]

I can resolve the error by writing d1 .> d2 = d2 .> d1 instead of using flip but I can't understand why flip won't work .我可以通过编写d1 .> d2 = d2 .> d1而不是使用flip来解决错误,但我不明白为什么 flip 不起作用 Any ideas?有任何想法吗?

Edit : removed second, unrelated question编辑:删除第二个不相关的问题

Haskell mandates that each equation for a function has the same number of explicit arguments on the left hand side. Haskell 要求函数的每个方程在左侧具有相同数量的显式参数。 This is not allowed:这是不允许的:

N .> S = ...   -- two arguments
W .> E = ...   -- two arguments
(.>) = ...     -- no arguments

Even if the last line is morally correct, since the type of the ... part has two arguments, Haskell does not allow it, since the arguments are not explicitly present in the left hand side.即使最后一行在道德上是正确的,因为...部分的类型有两个参数,Haskell 不允许这样做,因为参数没有明确出现在左侧。 So, we need to make the arguments explicit, using something like因此,我们需要使用类似的方法使参数明确

x .> y = ... x y  -- two arguments

That is:那是:

x .> y = flip (.>) x y

which can be simplified to可以简化为

x .> y = y .> x

which is what you wrote in the question.这就是您在问题中所写的内容。

If you wonder why this is disallowed, there is a question for that .如果您想知道为什么不允许这样做, 那么有一个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM