tags:

views:

70

answers:

2

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?

+6  A: 

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.

stusmith
Thanks, i get it.
demas
You meant list when you said array.
sepp2k
Cheers, fixed. I'm a C++ programmer by day, and my fingers were on auto-pilot.
stusmith
+1  A: 

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").

Konrad Rudolph