Bercovici Adrian
Bercovici Adrian

Reputation: 9360

Getting non-exhaustive pattern exception on method

I keep getting non-exhaustive pattern exception for the following method:

 groups::[Int]->[[Int]]
 groups ls=go ls [] [] where

        go [] small big=small:big
        go (x:xs) (y:ys) big | x==y = go xs (x:y:ys) big
                             | otherwise = go xs [] ((y:ys):big)

What i want to do is : given an array [1,2,3,3,4,4,4,1] i want to split it in list of consecutive duplicates: [[1],[2],[3,3],[4,4,4],[1]].

I am using 2 accumulators , one for the current forming list and the other for the big one.

I can not use wild-card neither for the big list neither for the small one , since the only situation that is unusual is the empty input list.

Upvotes: 0

Views: 34

Answers (1)

chepner
chepner

Reputation: 530843

You haven't accounted for something like go (x:xs) [] big; the only case that allows the second argument to be an empty list also requires the first argument to be an empty list as well.

    go [] small big=small:big
    go (x:xs) (y:ys) big | x==y = go xs (x:y:ys) big
                         | otherwise = go xs [] ((y:ys):big)
    go (x:xs) [] big = ???

Upvotes: 1

Related Questions