【发布时间】:2018-10-19 21:44:43
【问题描述】:
在下面的 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" 时,我收到错误“函数 allTransformations 中的非详尽模式”。问题出在哪里?
我已经介绍了四种情况:两个参数都是空字符串,第二个参数是空的,第一个不是,第一个参数是空的,第二个不是,两个参数都不为空。
这是所有可能的情况,对吧?
【问题讨论】:
-
拇指规则:如果保护块不以
otherwise结尾,则保护块之前的模式可能无法捕获所有情况,因此在检查穷举时可以忽略它。 (另外,打开警告,这样您就不必只在运行时捕获这些错误)
标签: haskell arguments pattern-matching