Reputation: 45295
I do:
Prelude> "sone" ++ "otehr"
"soneotehr"
But such code:
addOneToElement :: [a] -> [a]
addOneToElement element = element ++ "next"
main = do
let s = addOneToElement("some")
putStrLn s
produces this output:
all_possible_combinations.hs:22:37:
Couldn't match expected type `a' against inferred type `Char'
`a' is a rigid type variable bound by
the type signature for `addOneToElement'
at all_possible_combinations.hs:21:20
Expected type: [a]
Inferred type: [Char]
In the second argument of `(++)', namely `"next"'
In the expression: element ++ "next"
Why I get this error and how I can fix it?
Upvotes: 2
Views: 106
Reputation: 14103
Your type signature should be:
addOneToElement :: [Char] -> [Char]
(Or more simply, addOneToElement :: String -> String
)
The "a
" in your type signature is a wildcard - it can match anything. However you are trying to concatenate a list of Char
to a list of anything - and there is no way to do that.
Upvotes: 6
Reputation: 545628
Why are you using a type variable here anyway? The only type that can match is Char
, since the second operand of (++)
is fixed to [Char]
("next"
).
Upvotes: 1