Haskell 键入错误,出了什么问题?
我正在使用 Hugs 编译一个简单的 Haskell 函数来计算排列数。我希望它返回一个整数,但我需要对浮点数进行操作。 我尝试将答案计算为浮点型,然后将其截断,但由于某种原因它无法正常工作。
这是函数:
choose :: Float -> Float -> Integer
choose n r = truncate (chooseF (n r))
where
chooseF::Float->Float->Float
chooseF n r | n==r = 1
| otherwise = n / (n-r) * chooseF(n-1) r
这是错误(第 35 行是函数的第二行):
ERROR "/homes/mb4110/SimpleMath":35 - Type error in application
*** Expression : n r
*** Term : n
*** Type : Float
*** Does not match : a -> b
这可能是我遗漏的明显内容,但我已经研究了很长一段时间并且想不出解决方案。
I'm using hugs to compile a simple Haskell function calculating the number of permutations. I would like it to return an Integer, but I need to operate on floats.
I've tried to calculate the answer as a Float and then truncate it, but for some reason it's not working out.
This is the Function:
choose :: Float -> Float -> Integer
choose n r = truncate (chooseF (n r))
where
chooseF::Float->Float->Float
chooseF n r | n==r = 1
| otherwise = n / (n-r) * chooseF(n-1) r
This is the Error (Line 35 is the second line of the function):
ERROR "/homes/mb4110/SimpleMath":35 - Type error in application
*** Expression : n r
*** Term : n
*** Type : Float
*** Does not match : a -> b
It's probably something obvious that I'm missing but I've been at this for a good while and can't think of the solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
chooseF
采用两个参数,但由于括号n r
被解析为单个参数。因此,删除n r
周围的括号就可以了。chooseF
takes two arguments, but because of the parenthesisn r
is parsed as a single argument. Thus, remove the parenthesis aroundn r
and it should be fine.问题是您将
(nr)
传递给chooseF
。 Hugs 由此确定项n
必须是a -> 类型的某个函数。 b
,您要向其中传递r
。然后,其结果将部分应用于chooseF
。据推测,您希望使用
n
和r
作为参数来调用chooseF
。要修复此错误,请改为调用chooseF n r
。The problem is that you are passing
(n r)
tochooseF
. Hugs determines from this that the termn
must be some function of typea -> b
, into which you are passingr
. The result of this would then be partially applied intochooseF
.Presumably, you wanted to call
chooseF
with bothn
andr
as parameters instead. To fix this error, callchooseF n r
instead.