I'm trying to wrap my head around generics in Haskell. My issue is I created a concatenate function that takes a list of Strings and returns a giant string.
concatenate:: [String] -> String
concatenate xs = foldl (\acc x -> acc ++ x) [] xs
But now I'd like instead of a list of strings, use a list of anything. This can be strings, can be ints. So say
concatenate ["Phil", "is"]
generates "Philis" while
concatenate [[1,2],[3,4]]
generates [1,2,3,4].
I've found that
concatenate:: [a] -> a
concatenate xs = foldl (\acc x -> acc ++ x) [] xs
does not work and I'm not sure why. Isn't the way haskell works, whatever type a is, the output is a as well? Or is the issue with the second half not allowing it to work as a function for all types?
Occurs check: cannot construct the infinite type: a0 = [a0] In the first argument of '(++)', namely 'acc' In the expression: acc ++ x In the first argument of 'foldl', namely '(\ acc x -> acc ++ x)'This is after I've replaced [a] with [[a]] with the help of @Pubby 's useful tidbit on String. Is it trying to say that ++ won't work with the type a? Or that it's too general and ++ only works with lists? I'm kind of confused.ato[a]as Pubby pointed out?