嘿,伙计们,这是我的代码,在上面我得到了“多个镜像声明”的奇怪错误。在此之前我还有其他功能,但没有一个被命名为镜像......有什么想法吗?
mirror :: BinTree a -> BinTree a
mirror = undefined
mirror (Node tL x tR) = Node x mirror tR mirror tL
嘿,伙计们,这是我的代码,在上面我得到了“多个镜像声明”的奇怪错误。在此之前我还有其他功能,但没有一个被命名为镜像......有什么想法吗?
mirror :: BinTree a -> BinTree a
mirror = undefined
mirror (Node tL x tR) = Node x mirror tR mirror tL
一个函数的多个定义必须在等号左边有相同数量的参数。从理论的角度来看,这不是必需的(注意:其中一个定义当然可以是 lambda 或返回另一个函数),但人们似乎喜欢它,因为这样的定义通常表明存在错误。
具体来说,您有一个带有零参数的定义:
mirror = undefined
一个定义一个论点:
mirror (Node tL x tR) = Node x mirror tR mirror tL
你可能想要:
mirror _ = undefined
mirror (Node tL x tR) = Node x mirror tR mirror tL
This is not the issue with this specific example, but since this is the first result on Google for "multiple definitions Haskell", I figured I should contribute what the problem was for me:
If you are defining the function multiple times using pattern-matching with some of the arguments, all of the definitions must be consecutive. If there is other code in between them, they are considered separate definitions.
Example: the following is invalid because the definition of b
divides the definitions of a
:
frobnicate :: Bool -> String
frobnicate True = "foo"
b = "bar"
frobnicate False = b
第 2 行和第 3 行有冲突的类型:您已定义mirror
为 constant undefined
,然后尝试将其定义为单参数函数。删除第 2 行应该可以解决问题;不清楚你为什么首先写它。
您对mirror
. 第一条,
mirror = undefined
是包罗万象的,因此编译器认为定义已完成。然后考虑下一个子句开始一个新的定义。您应该删除该undefined
行。