Haskell (:) and (++) differences

Viewed 98658

I'm sorry for a question like this. I'm not too sure about the difference of the : and ++ operator in Haskell.

x:y:[] = [x,y]  

also

[x] ++ [y] = [x,y]

as for the reverse function which arose this question for me,

reverse ::[a]->[a]
reverse [] = []
reverse (x:xs) = reverse(xs)++[x]

Why doesn't the following work?

reversex ::[Int]->[Int]
reversex [] = []
reversex (x:xs) = reversex(xs):x:[]

giving a type error.

5 Answers

you can change a little and get the right result.

reversex ::[Int]->[Int] # comment this line
reversex [] = []
reversex (x:xs) = reversex(xs) ++ x : []
Related