0

I have a function, that clusters the consecutive numbers in a list, but I am getting an error stating

Non exhaustive patterns

causing the last set of consecutive integers not to print.

consecutive (w:v:zs) = help (w:v:zs) []
                              where
                                   help [] buffer =  []
                                   help (w:v:zs) buffer | (w+1 /= v) = (reverse (w:buffer)) : (help (v:zs) ([]))
                                                        | otherwise = (help (v:zs) (w:buffer)) 


Ok, one module loaded.
*HW1> consecutive [1,2,3,5,6,7,8,9,2,3,11,12]
[[1,2,3],[5,6,7,8,9],[2,3]*** Exception: HW1.hs:(108,36)-(110,94): Non-exhaustive patterns in function help
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
K raj
  • 3
  • 2

1 Answers1

1

You destructure the list (w:v:zs) and then immediately make a new list (w:v:zs). This does nothing except explicitly crash your function when given a list with fewer than two elements. Consider

consecutive zs = help zs []

Additionally, your help function handles the empty-list case and the case where a list has at least two elements, but not the case of a single element. So you need a help case for [x], which will likely behave similarly to your [] case (in either situation, there are too few elements left to compare for clustering).

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116