首页 文章

Haskell函数中的“非详尽模式”

提问于
浏览
0

在以下Haskell函数中:

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

运行 allTransformations "abc" "bca" 时出现错误"Non-exhaustive patterns in function allTransformations" . 问题出在哪儿?

我已经介绍了四种情况:两个参数都是空字符串,第二个参数是空的,第一个参数不是,第一个参数是空的而第二个参数不是,并且两个参数都不是空的 .

这是所有可能的情况,对吗?

1 回答

  • 4

    你实际上已经覆盖了六个案例,因为你提到的第四个案例有三个不同的警卫:

    • 两者都是空的

    • A是空的

    • B是空的

    • 两者都不是空的,第一个元素是相同的

    • 都不是空的,而A则更长

    • 两者都不是空的,而B则更长

    你错过了:

    • 都不是空的,第一个元素是不相等的,它们的长度相同

    最后一个是 "abc""bca" 的情况

相关问题