R – Haskell Noob question: What’s wrong with the append function

appendfunctional programminghaskell

I'm trying to write a Haskell append function… Here's what I have:

myappend :: [a] -> [a] -> [a]
myappend [] x = x
myappend [x:xs] y = x : myappend xs y

But it's giving me an error:
Occurs check: cannot construct the infinite type: a = [a]
When generalising the type(s) for `myappend'

So, obviously there's something wrong with it but I can't see it… What's wrong with my append function?

Best Answer

The constructors of the type [a] are:

[]  The empty list
(:) Cons operator (just a regular infix constructor)

So, you must use:

myappend :: [a] -> [a] -> [a]
myappend [] x = x
myappend (x:xs) y = x : myappend xs y

The syntax

[x:xs]

is pretty much equivalent to

[(x:xs)]

which matches a list with one element, a non-empty list, and has type

[(x:xs)] :: [[a]]

I recommend reading this page if you want to understand the constructor and pattern matching concept.