Non-exhaustive pattern in Haskell function

Viewed 60

I need to implement function that inserts two elements in the head of the List but I get

Exception: <interactive>:7:5-41: Non-exhaustive patterns in function addTwoElements

The code of the the function is following

addTwoElements a b [xs]= a : b : [xs]

Thanks in advance

1 Answers

A pattern like [xs] means you match only with lists that contain exactly one element (and that element is xs).

You can here use a variable xs for example and write the addTwoElements function like:

addTwoElements :: a -> a -> [a] -> [a]
addTwoElements a b xs = a : b : xs
Related