0
votes

How can I make sure the pattern

func (2:xs) = expression

where 2:xs is a length 2 list doesn't match with the pattern

func (2:x:xs) = expression2

where 2:x:xs is a length 3 list?

2
2:xs is a list of length at least 1, not of length 2. - Alexey Romanov

2 Answers

7
votes

Adapt this as needed:

func [] = ... -- empty case
func [x] = ... -- length=1 case
func [x,y] = ... -- length=2 case
func (x:y:z:zs) = ... -- length>=3 case
6
votes

End the list pattern with empty brackets:

func (2:x:[]) = expression

This will ensure x is a single element from the list.