Haskell常见报错及原因
前言
- haskell报错
FileName.hs: 62: 10
的错误会出现在FileName.hs
的第62
行及的第十个10
字符处。 - 本文的
balabala
指的是任意东西。 - 记得变量名/函数名用小写驼峰法
functionName
,data type名用大写驼峰法DataType
。
Couldn’t match excepted type ‘xxx’ with actual type ‘yyy’
同义报错Couldn't match type 'xxx' with actual type 'yyy'
错误原因:数据类型应该是xxx
但是你输入的是yyy
一般会出现在recursion的case expression里:_ -> balabala
Variable not in scope
错误原因一:未定义该变量名
错误原因二:haskell把function name当作variable name输入了,大概是少括号了
注意:记得(x:xs)
最好都加括号,不然也可能有Variable not in scope
的报错
Non-exhaustive patterns in function balabala
同义报错Non type-variable argument in the constraint: balabala
错误原因:没有输入的值符合的case,recursion里面debug会蛮困难
Parse error
Parse error (possibly incorrect indentation or mismatched brackets)
错误原因:缩进错了,或者少单个括号了。也可能是上一行少括号了。
Parse error in pattern: balabala
一般出现在functionName x:xs
,改成functionName (x:xs)
就好了。也可能是case/guarded缺胳膊少腿。
记得(x:xs)
最好都加括号,不然也可能有Variable not in scope
的报错
Parse error on input ‘|’ / ‘->’ / 变量名
错误原因:guarded/case expression写错了,或者缩进错了。或者加个括号
解决办法:检查一下‘|’
前面有没有写过=
,检查case是不是少了of,检查缩进是不是两个空格。
The constructor ‘balabala’ should have x arguments, but has been given x
错误原因:product data type少变量了
Ambiguous occurence ‘balabala’
错误原因:‘balabala’定义了多次,删一个就好了。如果一个是import from xxx,就说明在xxx文件中有另一个定义,当前文件也有一个定义。
Warning: [-Wincomplete-pattern]
case需要_ -> balabala
guarded需要otherwise = balabala