cb7
cb7

Reputation: 523

"Non-exhaustive patterns" in Haskell function

In the following Haskell function:

allTransformations :: String -> String -> [Transformation]
allTransformations "" "" = []
allTransformations a "" = [map (\a -> Delete a) a]
allTransformations "" b = [map (\b -> Insert b) b]
allTransformations (x:xs) (y:ys)
    | x == y = map (\t -> (Copy x) : t) rest
    | (length xs) < (length ys) = (map (\t -> (Insert y) : t) rest) ++ (map (\t -> (Change x y) : t) rest)
    | (length xs) > (length ys) = (map (\t -> (Delete x) : t) rest) ++ (map (\t -> (Change x y) : t) rest)
    where rest = allTransformations xs ys

When running allTransformations "abc" "bca" I get the error "Non-exhaustive patterns in function allTransformations". Where is the problem?

I've covered four cases: both arguments are empty strings, the second argument is empty and the first is not, the first argument is empty and the second is not, and neither argument is empty.

This is all the possible cases, right?

Upvotes: 0

Views: 151

Answers (1)

that other guy
that other guy

Reputation: 123470

You have actually covered six cases, because the fourth case you mention has three different guards:

  • Both are empty
  • A is empty
  • B is empty
  • Neither are empty, and the first elements are the same
  • Neither are empty, and A is longer
  • Neither are empty, and B is longer

You are missing:

  • Neither are empty, the first elements are unequal, and they are the same length

And that last one is exactly the case for "abc" and "bca"

Upvotes: 3

Related Questions