[Haskell-beginners] Question on : usage

akash g akaberto at gmail.com
Mon Dec 23 14:05:54 UTC 2013


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 <anguscomber at gmail.com> 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
>
>
> 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20131223/c4ccc8a6/attachment.html>


More information about the Beginners mailing list