I've been doing my annual attempt to learn Haskell this weekend, and as ever when I actually try to write a recursive function (rather than just type one in from a tutorial), I get a type error.
I'd very much appreciate any help understanding (1) what the error means (I don't understand the "fix"); and (2) why an error is being thrown at all - I'm fairly certain I'm not making any mistakes regarding the types being passed.
My code:
tell :: (Show a) => [a] -> String
tell'in :: (Show a, Num n) => [a] -> n -> String -> (n, String)
tell [] = "The list is empty"
tell (x:[]) = "The list has one element: " ++ show x
tell (x:xs) = "The list has " ++ n ++ " elements: " ++ s where (n, s) = (tell'in (xs) (1) (show x))
tell'in (x:[]) n s = ((n + 1), (s ++ " and " ++ (show x)))
tell'in (x:xs) n s = tell'in xs (n+1) (s ++ " and " ++ show x)
And the error I get when I try to load this into GHCI:
[1 of 1] Compiling Main ( example.hs, interpreted )
example.hs:13:88:
Could not deduce (Num [Char]) arising from the literal `1'
from the context (Show a)
bound by the type signature for tell :: Show a => [a] -> String
at example.hs:(11,1)-(13,99)
Possible fix:
add (Num [Char]) to the context of
the type signature for tell :: Show a => [a] -> String
or add an instance declaration for (Num [Char])
In the second argument of `tell'in', namely `(1)'
In the expression: (tell'in (xs) (1) (show x))
In a pattern binding: (n, s) = (tell'in (xs) (1) (show x))
Failed, modules loaded: none.
Prelude>