Type error when testing a function with a negative number

末鹿安然 提交于 2019-11-29 16:07:04
leftaroundabout

As already discussed in the comments, this is simply a matter of parsing rules. Your expression take' -2 [2] sure looks like it should mean take' (-2) [2] as you intend. And arguably, it should be parsed as just that. In fact GHC has an extension to achieve that behaviour:

GHCi, version 7.10.2: http://www.haskell.org/ghc/  :? for help
Prelude> :set -XNegativeLiterals 
Prelude> take -2 [2]
[]

By default however, Haskell always first tries to parse all operators, including -, as infix operators. In the above expression, - has both something to the left and to the right (albeit with inconsistent spacing, but that's ignored), so without -XNegativeLiterals this ends up getting parsed as (take) - (2 [2]), which means something completely different. Really it's just utter bogus, as the error message suggests in a really cryptic way: it wants Num (i -> [a] -> [a]), i.e. it finds your code requires to treat a function (namely, take) as a number (namely, as an argument to the subtraction operator).

Almost always when you see an error including demand for Num (Some Compound Type) it means something is completely wrong already at the parsing level.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!