
I am playing with concatenating lists so I start with this: testconcat :: [[a]] -> [a] testconcat [[]] = [] testconcat [(x:xs)] = x : xs Now, testconcat [[]] and testconcat [[1,2]] works So now I want to try with a 2nd inner list so I do this: testconcat :: [[a]] -> [a] testconcat [[]] = [] testconcat [(x:xs)] = x : xs testconcat [(x:xs), (y:ys)] = x : xs : y : ys and I get load error: Couldn't match expected type `a' with actual type `[a]' `a' is a rigid type variable bound by the type signature for testconcat :: [[a]] -> [a] at prog_haskell.hs:218:15 In the first argument of `(:)', namely `xs' In the second argument of `(:)', namely `xs : y : ys' In the expression: x : xs : y : ys But this loads ok: testconcat :: [[a]] -> [a] testconcat [[]] = [] testconcat [(x:xs)] = x : xs testconcat [(x:xs), (y:ys)] = x : xs Even this line cannot be loaded: testconcat [(x:xs), (y:ys)] = x : xs : [] Couldn't match expected type `a' with actual type `[a]' `a' is a rigid type variable bound by the type signature for testconcat :: [[a]] -> [a] at prog_haskell.hs:218:15 In the first argument of `(:)', namely `xs' In the second argument of `(:)', namely `xs : []' In the expression: x : xs : [] Failed, modules loaded: none. if x : xs works why not x : xs : <something else> ??? Can someone please explain?

On Mon, Dec 23, 2013 at 8:55 AM, Angus Comber
Couldn't match expected type `a' with actual type `[a]' `a' is a rigid type variable bound by the type signature for testconcat :: [[a]] -> [a] at prog_haskell.hs:218:15 In the first argument of `(:)', namely `xs' In the second argument of `(:)', namely `xs : []' In the expression: x : xs : [] Failed, modules loaded: none.
if x : xs works why not x : xs : <something else> ???
x is an element. xs is a list of those elements. x : xs prepends a single element to a list. x : xs : y would attempt to prepend a single element *and* a list to something else --- but if you deconstructed a list to get x and xs, then x and xs are not the same type (since xs's type is that of a list of x). Perhaps the thing to understand is that in Haskell, a list is built up of elements all of the same type using (:): [x,y,z] is the same as x : y : z : [] (If you're familiar with Lisp, (:) is exactly cons and [] is nil.) But because Haskell is strictly typed, a list must contain elements all the same type. So you can't have y in that be a list of the same type as x, it must be a value the same type as x. Maybe you are looking for (++) instead? But note that that takes lists, not items, so it would have to be ([x] ++ xs ++ ...). -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Because the type of cons (:) is a -> [a] -> [a].
Thus, when you do list : something else, you get a type mismatch. You are
basically giving it [a] -> [a] -> [a].
You got yourself a type violation.
On Mon, Dec 23, 2013 at 7:25 PM, Angus Comber
I am playing with concatenating lists so I start with this:
testconcat :: [[a]] -> [a] testconcat [[]] = [] testconcat [(x:xs)] = x : xs
Now, testconcat [[]] and testconcat [[1,2]] works
So now I want to try with a 2nd inner list so I do this:
testconcat :: [[a]] -> [a] testconcat [[]] = [] testconcat [(x:xs)] = x : xs testconcat [(x:xs), (y:ys)] = x : xs : y : ys
and I get load error: Couldn't match expected type `a' with actual type `[a]' `a' is a rigid type variable bound by the type signature for testconcat :: [[a]] -> [a] at prog_haskell.hs:218:15 In the first argument of `(:)', namely `xs' In the second argument of `(:)', namely `xs : y : ys' In the expression: x : xs : y : ys
But this loads ok: testconcat :: [[a]] -> [a] testconcat [[]] = [] testconcat [(x:xs)] = x : xs testconcat [(x:xs), (y:ys)] = x : xs
Even this line cannot be loaded: testconcat [(x:xs), (y:ys)] = x : xs : []
Couldn't match expected type `a' with actual type `[a]' `a' is a rigid type variable bound by the type signature for testconcat :: [[a]] -> [a] at prog_haskell.hs:218:15 In the first argument of `(:)', namely `xs' In the second argument of `(:)', namely `xs : []' In the expression: x : xs : [] Failed, modules loaded: none.
if x : xs works why not x : xs : <something else> ???
Can someone please explain?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On 12/23/2013 08:55 AM, Angus Comber wrote:
I am playing with concatenating lists so I start with this:
testconcat :: [[a]] -> [a] testconcat [[]] = [] testconcat [(x:xs)] = x : xs
Now, testconcat [[]] and testconcat [[1,2]] works
So now I want to try with a 2nd inner list so I do this:
testconcat :: [[a]] -> [a] testconcat [[]] = [] testconcat [(x:xs)] = x : xs testconcat [(x:xs), (y:ys)] = x : xs : y : ys
and I get load error: Couldn't match expected type `a' with actual type `[a]' `a' is a rigid type variable bound by the type signature for testconcat :: [[a]] -> [a] at prog_haskell.hs:218:15 In the first argument of `(:)', namely `xs' In the second argument of `(:)', namely `xs : y : ys' In the expression: x : xs : y : ys
The colon operator (:) takes a single element on the left, and a list on the right. In, testconcat [(x:xs), (y:ys)] = x : xs : y : ys you've got lists on both the left and the right. This would work: testconcat [(x:xs), (y:ys)] = x : ( y : (xs ++ ys) ) (note: not the same function as you wanted!) since it keeps all of the single-elements on the left-hand side of (:). You're going to need to use (++) in at least one place, though, since you need to combine the lists 'xs' and 'ys' somehow. The function you really want is, testconcat [(x:xs), (y:ys)] = (x:xs) ++ (y:ys) but of course this is just, testconcat [l1, l2] = l1 ++ l2
participants (5)
-
akash g
-
Angus Comber
-
Brandon Allbery
-
Daniel Trstenjak
-
Michael Orlitzky