我一直在這段代碼中運行了將近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