Haskell 对函数调用感到沮丧
学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的问题是
main
必须是IO a
形式的类型(您可以在错误中看到这一点 - GHC 期望 main 的推断类型[[ a0]]
来匹配IO t0
)。这里main
的类型为[[Int]]
。您只需打印结果即可轻松解决此问题:Your problem here is that
main
must be of a type of the formIO a
(you can see this in the error - GHC is expecting main's inferred type[[a0]]
to match againstIO t0
). Heremain
has type[[Int]]
. You can easily fix this by simply printing the result:函数
main
必须具有类型IO a
。您将 main 定义为testfn [..]
,其类型为Ord a, Num a =>; [[a]]
。你想让程序做什么?与您订购的已知解决方案进行比较?
或者打印结果?
The function
main
must have typeIO a
. You are defining main astestfn [..]
which is of typeOrd a, Num a => [[a]]
.What do you want the program to do? Compare to a known solution for your ordering?
Or perhaps print the result?