Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 有关"的问题,;变量不在范围内“;及;例外情况:<;交互式>;:“函数中的非穷举模式”;_Haskell - Fatal编程技术网

Haskell 有关"的问题,;变量不在范围内“;及;例外情况:<;交互式>;:“函数中的非穷举模式”;

Haskell 有关"的问题,;变量不在范围内“;及;例外情况:<;交互式>;:“函数中的非穷举模式”;,haskell,Haskell,运行以下代码时,我遇到错误“变量不在范围内:其他::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:

运行以下代码时,我遇到错误“变量不在范围内:其他::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>
结果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
Prelude>subst[uuu[]=[]
前奏曲>子片段“”(x:xs)=(x:xs)
前奏曲>子a b(x:xs)| x==a=b:subst a b xs | x/=a=x:subst a b xs
前奏曲>第20小节[1,2,0,1]
[1,0,0,1***异常::92:1-72:函数subst中的非穷举模式
有人能帮我解释和解决上面的问题吗

多谢各位


Cheers

在第一个示例中,将
其他
替换为
其他
。在第二个示例中,您没有为其输入的所有可能值定义的total函数,尤其是缺少一个元素
subst a b[x]列表的函数定义=--将函数定义放在这里

@jaroslawj已经解释了如何解决第一个问题。关于第二个问题,我看不到代码的问题(模式对我来说很详尽)-您能告诉我在出现错误时您是如何调用函数的吗?为什么在每个定义的第一行开头有一个
*
。您是从文件加载这些函数还是将它们直接放入GHCi?在后一种情况下,您需要
:{
开头表示这是一个多行定义,
}:
最后。@Robin Zigmond抱歉,我只是复制并粘贴了代码。原始代码没有*符号。当我将另一个代码替换为*符号时,它仍然会报告错误。请您编辑问题以显示您正在运行的确切代码,好吗?@Robin Zigmond,我已经更新了代码和结果。非常感谢很多。我浪费了一个下午去寻找原因,但没有什么用处。:(另外,它现在为
unroll
显示的特定错误是因为:
y:unroll x-1 ys
,您缺少必要的括号,它应该是
y:(unroll(x-1)ys)
。[我还强烈建议将每个防护装置放在一条单独的线上,当它们都在一条线上时,很难读取。]对不起,在我将另一条换成另一条后,它仍然会报告错误。
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