我在运行以下代码时遇到错误“ 变量不在范围内: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
有人可以帮我解释和解决以上问题吗?
非常感谢您!
欢呼
答案 0 :(得分:0)
在第一个示例中,将other
替换为otherwise
。在第二个示例中,您没有针对其输入的所有可能值定义的总计函数-特别是您缺少具有一个元素subst a b [x] = -- place function definition here