Haskell中类型声明中的列表
我已经在这段代码上写了将近2个小时了,我一直收到同样的编译器错误消息。我已经做了研究,但就是找不到答案Haskell中类型声明中的列表,haskell,Haskell,我已经在这段代码上写了将近2个小时了,我一直收到同样的编译器错误消息。我已经做了研究,但就是找不到答案 buildTable :: Int -> Int -> (Int -> Int -> a) -> [[a]] buildTable n m fun = [[ fun x y | x <- [0..n-1]] | y <- [0..m-1]] lookupAns ::
buildTable :: Int -> Int -> (Int -> Int -> a) -> [[a]]
buildTable n m fun = [[ fun x y
| x <- [0..n-1]]
| y <- [0..m-1]]
lookupAns :: Int -> Int -> [[Int]] -> Int
lookupAns len1 len2 theArray =
theArray !! len1 !! len2
lcsLength :: String -> String -> Int
lcsLength s1 s2 =
let
n1 = (length s1)
n2 = (length s2)
table = buildTable (n1 n2 lcsHelp)
lcsHelp = if ( n1 == 0 || n2 == 0 )
then 0
else if ( last s1 == last s2 )
then
(lookupAns
(n1 - 1)
n2
table)
+ 1
else
max
(lookupAns
n1
(n2-1)
table)
(lookupAns
(n1-1)
n2
table)
in lookupAns
(length s1)
(length s2)
table
buildTable::Int->Int->(Int->Int->a)->[[a]]
构建表n m fun=[[fun x y
|x[[Int]]->Int
lookupAns len1 len2阵列=
阵列!!镜头1!!镜头2
lcsLength::String->String->Int
LCS长度s1 s2=
让
n1=(长度s1)
n2=(长度s2)
表=构建表(n1 n2 lcsHelp)
lcsHelp=if(n1==0 | | n2==0)
然后0
else if(最后一个s1==最后一个s2)
然后
(lookupAns)
(n1-1)
氮气
表1)
+ 1
其他的
最大值
(lookupAns)
n1
(n2-1)
表1)
(lookupAns)
(n1-1)
氮气
表1)
在lookupAns
(长度s1)
(长度s2)
桌子
现在,无论我尝试什么,我都会收到相同的错误消息。错误消息是“无法将预期类型“[[Int]]->Int”与实际类型[Int]”匹配,其他规范指向代码末尾的第一个max调用。请帮助,这确实令人沮丧
它现在使用我的新代码进行编译和运行。由于时间有点晚,我一定会稍后发布它,我会把它记下来过夜。lcslength中的第一个lookupan应用于太少的参数。lcslength中的第一个lookupan应用于太少的参数。这是错误的:
table = buildTable (n1 n2 lcsHelp)
buildTable
的类型为Int->Int->(Int->Int->a)->[[a]]
buildTable(n1 n2 lcsHelp)
将其应用于一个参数,即(n1 n2 lcsHelp)
。因此表的类型为Int->(Int->Int->Int->a)->[[a]]
,作为第三个参数传递给查找程序是无效的
尽管(n1 n2 lcsHelp)
试图将整数n1
应用于两件事,这显然是垃圾
但是,我没有收到您引用的错误消息。GHCi给了我:
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main ( bar.hs, interpreted )
bar.hs:18:13:
Couldn't match expected type `[[Int]]'
with actual type `Int -> (Int -> Int -> a0) -> [[a0]]'
In the return type of a call of `buildTable'
In the expression: buildTable (n1 n2 lcsHelp)
In an equation for `table': table = buildTable (n1 n2 lcsHelp)
我不确定这是否是因为您发布的代码实际上不是您为获取错误消息而编译的代码(这是由您必须更正输入错误这一事实所暗示的),或者只是GHCi在与您使用的编译器不同的位置发现了不一致性
我猜你的意思可能是:
table = buildTable n1 n2 lcsHelp
但这又给了我一个不同的错误。这是错误的:
table = buildTable (n1 n2 lcsHelp)
buildTable
的类型为Int->Int->(Int->Int->a)->[[a]]
buildTable(n1 n2 lcsHelp)
将其应用于一个参数,即(n1 n2 lcsHelp)
。因此表的类型为Int->(Int->Int->Int->a)->[[a]]
,作为第三个参数传递给查找程序是无效的
尽管(n1 n2 lcsHelp)
试图将整数n1
应用于两件事,这显然是垃圾
但是,我没有收到您引用的错误消息。GHCi给了我:
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main ( bar.hs, interpreted )
bar.hs:18:13:
Couldn't match expected type `[[Int]]'
with actual type `Int -> (Int -> Int -> a0) -> [[a0]]'
In the return type of a call of `buildTable'
In the expression: buildTable (n1 n2 lcsHelp)
In an equation for `table': table = buildTable (n1 n2 lcsHelp)
我不确定这是否是因为您发布的代码实际上不是您为获取错误消息而编译的代码(这是由您必须更正输入错误这一事实所暗示的),或者只是GHCi在与您使用的编译器不同的位置发现了不一致性
我猜你的意思可能是:
table = buildTable n1 n2 lcsHelp
但这又给了我一个不同的错误。我粘贴了代码,以便更容易发现问题。正如@Ben已经指出的,问题是表的类型
buildTable
函数的类型为Int->(Int->Int->a)->[[a]]
。您将其称为table=buildTable(n1 n2 lcsHelp)
。因此,table
的类型将是Int->(Int->Int->a)->[[a]]
。此类型传递给具有类型Int->Int->Int->[[Int]]->Int的lookupAns
函数无效
如果您要执行类似于table=buildTable n1 n2 lcsHelp
的操作,我相信这可能是您的意图,那么buildTable
的类型签名必须更改,因为您将遇到此错误
Couldn't match expected type `Int -> Int -> Int'
with actual type `Int'
In the third argument of `buildTable', namely `lcsHelp'
这是因为现在lcsHelp
函数返回一个Int
(因为来自if..else
语句的返回值)与buildTable
函数的实际类型不匹配
因此,如果你能解释一下你想要实现的目标,这将更容易帮助你。很可能你需要重新访问lcsHelp
函数的类型。可能是buildTable
函数不需要将函数作为输入参数。我粘贴了代码,以便更容易找到iss正如@Ben已经指出的,问题在于表的类型
buildTable
函数的类型为Int->(Int->Int->a)->[[a]]
。您将其称为table=buildTable(n1 n2 lcsHelp)
。因此,table
的类型将是Int->(Int->Int->a)->[[a]]
。此类型传递给具有类型Int->Int->Int->[[Int]]->Int的lookupAns
函数无效
如果您要执行类似于table=buildTable n1 n2 lcsHelp
的操作,我相信这可能是您的意图,那么buildTable
的类型签名必须更改,因为您将遇到此错误
Couldn't match expected type `Int -> Int -> Int'
with actual type `Int'
In the third argument of `buildTable', namely `lcsHelp'
这是因为现在的lcsHelp
函数返回一个Int
(因为t