So I realize this is a possible duplicate question, as there a number of those errors reported on Stack Overflow, but none of the solutions seem to apply to my problem.
So I have the following function:
elementAt' :: Integral b => [a] -> b -> a
elementAt' [x:_] 1 = x
elementAt' [x:xs] y = elementAt' xs yminus1
where yminus1 = y - 1
In case you're wondering it's problem 3 from 99 Haskell Problems. The goal of the function is to take as input a list and an index, and return the value at that index (starting at 1). I don't want a solution to the problem, if I did I could just look at the ones provided. But I'm getting an error I don't understand. I'm using eclipseFP, the eclipse plugin for haskell and it's underlining the "[x:_]" and "[x:xs]" portions of the function with the following error:
Couldn't match type `a' with `[a]'
`a' is a rigid type variable bound by
the type signature for elementAt' :: Integral b => [a] -> b -> a
In all the threads that discuss this error that I've looked at the problem usually occurs when someone tries to give an incorrect output to something which expects a certain type. For example, returning the length of something (which is of type Int) to what should be a "Num a" variable type.
But in my case I'm not even providing a type for variable a. It should be able to be ANYTHING, right? So why am I getting this error? If I understood why I was getting the error I could fix it, but I just don't understand.
Could someone please explain to me why I'm receiving this error?
Your help is much appreciated, thank you. -Asaf
Edit: Every answer provided so far is correct, thank you all for the helpful information. I'm going to pick the one I believe to be most clear (I have to wait 5 minutes to do it though).
elementAt' [x:xs] y = elementAt' xs (y-1)
– Viktor Mellgren