有关“变量不在范围内”和“异常:<interactive>:函数中的非穷举模式”的问题

时间:2019-03-25 10:12:25

标签: haskell

我在运行以下代码时遇到错误“ 变量不在范围内:other :: Bool

unroll x [] = []
unroll 0 y = []
unroll x (y:ys) | x > length (y:ys) = y:unroll x-1 ys | otherwise = take x (y:ys)

结果:

Prelude>     unroll x [] = []
Prelude>     unroll 0 y = []
Prelude>     unroll x (y:ys) | x > length (y:ys) = y:unroll x-1 ys | otherwise = take x (y:ys)

<interactive>:89:5: error:
    ? Couldn't match type ‘[a] -> [a]’ with ‘[a]’
      Expected type: Int -> [a]
        Actual type: Int -> [a] -> [a]
    ? Relevant bindings include
        unroll :: Int -> [a] (bound at <interactive>:89:5)
Prelude>

另一个问题是“ ******异常:: 12:1-72:函数subst ***中的非穷尽模式”在运行第二个代码时跳出了

subst _ _ [] = []
subst "" "" (x:xs) = (x:xs)
subst a b (x:xs) | x == a = b : subst a b xs | x /= a = x : subst a b xs*

结果2:

Prelude> subst _ _ [] = []
Prelude> subst "" "" (x:xs) = (x:xs)
Prelude> subst a b (x:xs) | x == a = b : subst a b xs | x /= a = x : subst a b xs
Prelude> subst 2 0 [1,2,0,1]
[1,0,0,1*** Exception: <interactive>:92:1-72: Non-exhaustive patterns in function subst

有人可以帮我解释和解决以上问题吗?

非常感谢您!

欢呼

1 个答案:

答案 0 :(得分:0)

在第一个示例中,将other替换为otherwise。在第二个示例中,您没有针对其输入的所有可能值定义的总计函数-特别是您缺少具有一个元素subst a b [x] = -- place function definition here

的列表的函数定义