标签: multiline-repl-definition

函数 len 中的非穷举模式

我正在编写这个函数 len 来计算 GHCi 中列表的长度。

len [] = 0
len [x] = 1
len (x:xs) = 1 + len xs
Run Code Online (Sandbox Code Playgroud)

我试图用[]作为参数调用该函数,但错误Exception: Non-exhaustive patterns in function len击中了我。我不是已经在函数定义中包含了空列表案例吗?

haskell ghci non-exhaustive-patterns read-eval-print-loop multiline-repl-definition

1
推荐指数
2
解决办法
115
查看次数

插入功能中缺少模式

我有这个功能inserts,其中

inserts 1 [2,3] = [[1,2,3],[2,1,3],[2,3,1]]
Run Code Online (Sandbox Code Playgroud)

这是定义(直接来自Bird和Gibbons的Haskell算法设计)

inserts :: a -> [a] -> [[a]]
inserts x [] = [[x]]
inserts x (y:ys) = (x:y:ys) : map (y:) (inserts x ys)
Run Code Online (Sandbox Code Playgroud)

我已经用上面的例子在 ghci 中尝试过,但我得到以下异常

[[1,2,3],[2,1,3]*** Exception: <interactive>:2:1-53: Non-exhaustive patterns in function inserts
Run Code Online (Sandbox Code Playgroud)

有谁知道缺少的模式是什么?

haskell ghci non-exhaustive-patterns multiline-repl-definition

1
推荐指数
1
解决办法
59
查看次数