无法匹配预期的类型Haskell
我需要制作一个像[B,N,N]
这样的例子列表,其中列表包含N
的索引,如[1,2]
,以及表示最终列表长度的int。无法匹配预期的类型Haskell
这是我的计划:
lignebn :: Int -> [Int] -> [Case]
lignebn i [] = []
lignebn i x
| i == last x = [N] : lignebn (i-1) init x -- line 101
| otherwise = [B] : lignebn (i-1) init x
下面是错误:
devoir.hs:101:17:
Couldn't match expected type ‘[Int]’ with actual type ‘Int’
In the first argument of ‘last’, namely ‘(x)’
In the second argument of ‘(==)’, namely ‘last (x)’
devoir.hs:101:29:
Couldn't match expected type ‘Int -> [Case]’
with actual type ‘[Case]’
The function ‘lignebn’ is applied to three arguments,
but its type ‘Int -> [Int] -> [Case]’ has only two
In the second argument of ‘(++)’, namely ‘lignebn (i - 1) init (x)’
In the expression: [N] ++ lignebn (i - 1) init (x)
devoir.hs:101:43:
Couldn't match expected type ‘[Int]’
with actual type ‘[a0] -> [a0]’
Probable cause: ‘init’ is applied to too few arguments
In the second argument of ‘lignebn’, namely ‘init’
In the second argument of ‘(++)’, namely ‘lignebn (i - 1) init (x)’
devoir.hs:102:26:
Couldn't match expected type ‘Int -> [Case]’
with actual type ‘[Case]’
The function ‘lignebn’ is applied to three arguments,
but its type ‘Int -> [Int] -> [Case]’ has only two
In the second argument of ‘(++)’, namely ‘lignebn (i - 1) init (x)’
In the expression: [B] ++ lignebn (i - 1) init (x)
devoir.hs:102:40:
Couldn't match expected type ‘[Int]’
with actual type ‘[a1] -> [a1]’
Probable cause: ‘init’ is applied to too few arguments
In the second argument of ‘lignebn’, namely ‘init’
In the second argument of ‘(++)’, namely ‘lignebn (i - 1) init (x)’
Failed, modules loaded: none.
我不明白这是怎么回事,该代码似乎确定我...
在递归调用中,需要围绕init x
的括号。事实上,您正在通过init
函数和x
。您可能还需要N
和B
列表,而不是列表中的列表,但很难肯定地说,因为您没有包含您的Case
类型的定义。
lignebn :: Int -> [Int] -> [Case]
lignebn i [] = []
lignebn i x
| i == last x = N : lignebn (i-1) (init x) -- line 101
| otherwise = B : lignebn (i-1) (init x)
还要注意像last
那部分功能被广泛气馁。更好的是这样的(未经测试):
unsnoc :: [a] -> Maybe ([a], a)
unsnoc [] = Nothing
unsnoc (x : xs) = Just ps where
ps = case unsnoc xs of
Nothing -> ([], x)
Just (~(ys, y)) -> Just (x : ys, y)
last
期望列表,但是您已经在模式匹配中将其定义为并且Int
lignebn :: Int -> [Int] -> [Case]
lignebn i (x:xs) = ....
意味着
(x:xs)::[Int]
这意味着x是一个Int。
在一个不相关的音符,在Haskell你不需要把函数调用前后的括号。不要像这样打电话给last(x)
,而应该使用last x
。它会工作,但不是惯用的哈斯克尔。
我编辑了我的代码,但错误是相同的。对我来说,我的'i'是'Int',我的'x'是'[Int]',输出是'[Case]'。也许我的语法错了? – pioupiou1211
我不知道你应用了哪些编辑,但我可以告诉你这一点....从你上面写的内容来看,它是'(x:xs)',而不是''[Int]类型''' 。如果这不是你想要的,你将不得不通过改变类型或模式匹配来改变它。 – jamshidh
是的,这是我想要的,但我不明白错误来自哪里... – pioupiou1211
感谢它很明显! – pioupiou1211