Haskell 键入错误,出了什么问题?

发布于 2024-12-08 15:34:39 字数 644 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦亿 2024-12-15 15:34:39

chooseF 采用两个参数,但由于括号 n r 被解析为单个参数。因此,删除 n r 周围的括号就可以了。

chooseF takes two arguments, but because of the parenthesis n r is parsed as a single argument. Thus, remove the parenthesis around n r and it should be fine.

清秋悲枫 2024-12-15 15:34:39

问题是您将 (nr) 传递给 chooseF。 Hugs 由此确定项 n 必须是 a -> 类型的某个函数。 b,您要向其中传递 r。然后,其结果将部分应用于chooseF

据推测,您希望使用 nr 作为参数来调用 chooseF。要修复此错误,请改为调用 chooseF n r

The problem is that you are passing (n r) to chooseF. Hugs determines from this that the term n must be some function of type a -> b, into which you are passing r. The result of this would then be partially applied into chooseF.

Presumably, you wanted to call chooseF with both n and r as parameters instead. To fix this error, call chooseF n r instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文