我複製了 (http://www.haskell.org/haskellwiki/Euler_problems/11_to_20#Problem_11)給出的Euler.11解決方案,但它失敗並出現索引錯誤:「(Array。!):undefined array element」。 當然,首先我想要一個更好的錯誤消息(!),甚至可能會提供失敗的索引,但如果失敗,我嘗試調試它。調試列表理解
的數據被輸入正確,並且它的一個打印示出右界限,和數據。
所以我增加了一些跟蹤消息,這兩個結果的表達和理解的身體。我從最終表達式中得到很多跟蹤結果,但沒有一個來自身體計算。爲什麼?
prods :: Array (Int, Int) Int -> [Int]
-- trace ("xs: " ++ show xs) (product xs)
prods a = [trace ("xs: " ++ show xs) (product xs) | i <- range $ bounds a,
s <- senses,
let trace1 = check "i: " i,
let is = take 4 $ iterate s i,
let trace2 = check "is: " is,
all (inArray a) is,
let xs = map (a!) is]
-- Doit
-- euler = print . maximum . prods . input =<< getContents
euler eData = maximum . prods $ input eData
-- Debugging tracecheck :: String -> a -> a
check msg v | trace (msg ++ (show v)) True = v
非常感謝所有觀點;似乎很明顯(現在!)。我在構建完數組後打印了這個數組,它看起來都很好,並且具有預期的邊界。我會多花些時間看看它是如何得到一些未定義的值。 – guthrie
你對數據的錯誤設置是正確的 - 我錯誤地解釋了錯誤信息。 – guthrie