2
votes

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'

2
Note that [a] is a list, not an array. The difference is quite important; haskell has both, and they have very different performance implicationsbdonlan

2 Answers

7
votes

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]])
3
votes

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]]