Fedo
Fedo

Reputation: 349

Non-exhaustive pattern in Haskell function

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

Upvotes: 2

Views: 68

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476557

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

Upvotes: 6

Related Questions