[英]Haskell frustration with function calls
Learning Haskell is killing me. 学习Haskell正在杀了我。 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'
无法将预期类型'IO t0'与实际类型'[[a0]]'匹配在表达式中:main检查函数'main'的类型时
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
). 你的问题是
main
必须是IO a
形式的IO a
(你可以在错误中看到这一点 - GHC期望main的推断类型[[a0]]
与IO t0
匹配)。 Here main
has type [[Int]]
. 这里
main
有类型[[Int]]
。 You can easily fix this by simply printing the result: 您只需打印结果即可轻松解决此问题:
main = print (testfn [[1],[2]])
The function main
must have type IO a
. 函数
main
必须具有类型IO a
。 You are defining main as testfn [..]
which is of type Ord a, Num a => [[a]]
. 您将main定义为
testfn [..]
,其类型为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]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.