【发布时间】:2021-02-01 11:55:53
【问题描述】:
我有这个功能inserts where
inserts 1 [2,3] = [[1,2,3],[2,1,3],[2,3,1]]
这是定义(直接来自 Bird 和 Gibbons 的 Haskell 算法设计)
inserts :: a -> [a] -> [[a]]
inserts x [] = [[x]]
inserts x (y:ys) = (x:y:ys) : map (y:) (inserts x ys)
我已经用上面的例子在 ghci 中尝试过了,但是我得到了以下异常
[[1,2,3],[2,1,3]*** Exception: <interactive>:2:1-53: Non-exhaustive patterns in function inserts
有谁知道缺失的模式是什么?
【问题讨论】:
-
inserts n = zipWith (\xs ys -> xs (n:ys)) inits tails
-
即
inserts n xs = [ a ++ n:b | (a,b) <- zip (inits xs) (tails xs) ] -
另外,this answer 与主题有关(不是错误)。 (免责声明,这是我写的)。
标签: haskell ghci non-exhaustive-patterns multiline-repl-definition