[Haskell-beginners] Question on : usage

Brandon Allbery allbery.b at gmail.com
Mon Dec 23 14:03:46 UTC 2013


On Mon, Dec 23, 2013 at 8:55 AM, Angus Comber <anguscomber at gmail.com> wrote:

>     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 at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20131223/66ee5b80/attachment.html>


More information about the Beginners mailing list