我一直在这段代码中运行了将近2个小时,并且不断收到相同的编译器错误消息。我已经完成了我的研究,但找不到答案Haskell中类型声明中的列表列表
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
现在无论我尝试什么,我都会得到相同的错误消息。错误消息是“无法匹配期望的类型”[[Int]] - > Int',其实际类型为[Int]“其他规范指向代码末尾的第一个max调用。请帮忙,这真的令人沮丧
它现在编译并运行我的新代码。我会确保稍后发布它,因为它迟到了,我会把它放下来过夜。
注意名单对于这种类型的东西并不是很好,因为它们在空间方面有很大的开销,而且_O(n)_索引很慢。对于小案例或原型,它们都可以,但对于任何严重的问题,您应该使用数组或矢量来代替。 – hammar 2012-04-12 04:29:26