Haskell 对函数调用感到沮丧

发布于 2024-12-05 06:57:33 字数 297 浏览 1 评论 0原文

学习 Haskell 简直要了我的命。如果我要编写一个函数,它接受可排序元素数组的数组,并输出相同的内容,我该怎么做?

我尝试:

main = testfn [[1],[2]]

testfn :: (Ord a) => [[a]] -> [[a]]
testfn x = x

但我得到的信息是:

无法将预期类型“IO t0”与实际类型“[[a0]]”匹配 表达式:main 当检查函数'main'的类型时

Learning Haskell is killing me. If I am going to write a function that takes an array of arrays of order-able elements, and outputs the same thing, how do I do that?

I try:

main = testfn [[1],[2]]

testfn :: (Ord a) => [[a]] -> [[a]]
testfn x = x

But the message I get is:

Couldn't match expected type 'IO t0' with actual type '[[a0]]' In the
expression: main When checking the type of the function 'main'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

用心笑 2024-12-12 06:57:33

这里的问题是 main 必须是 IO a 形式的类型(您可以在错误中看到这一点 - GHC 期望 main 的推断类型 [[ a0]] 来匹配 IO t0)。这里 main 的类型为 [[Int]]。您只需打印结果即可轻松解决此问题:

main = print (testfn [[1],[2]])

Your problem here is that main must be of a type of the form IO a (you can see this in the error - GHC is expecting main's inferred type [[a0]] to match against IO t0). Here main has type [[Int]]. You can easily fix this by simply printing the result:

main = print (testfn [[1],[2]])
尐偏执 2024-12-12 06:57:33

函数 main 必须具有类型 IO a。您将 main 定义为 testfn [..] ,其类型为 Ord a, Num a =>; [[a]]

你想让程序做什么?与您订购的已知解决方案进行比较?

main = print (knownSolution == testfn [[1],[2]])

或者打印结果?

main = print $ testfn [[1],[2]]

The function main must have type IO a. You are defining main as testfn [..] which is of type Ord a, Num a => [[a]].

What do you want the program to do? Compare to a known solution for your ordering?

main = print (knownSolution == testfn [[1],[2]])

Or perhaps print the result?

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