2015-02-11 80 views
0

爲什麼max prod nextProd的類型在下面的函數中被推斷爲Int->Int->Int而不僅僅是Int?Haskell中的奇怪編譯錯誤

import qualified Data.Vector as DV 

largestProduct::DV.Vector Int->(Int, Int) 
largestProduct digits = foldl lastProdAndMax (0,0) [1..((DV.length digits) - 13)] where 
    lastProdAndMax _ 1 = (first, first) where first = DV.foldl (*) 1 $ DV.take 13 digits 
    lastProdAndMax (prod, max) i = (nextProd, max prod nextProd) 
     where nextProd = (prod `div` ((DV.!) digits (i-13))) * ((DV.!) digits i) 

回答

3

你的錯誤是在

lastProdAndMax (prod, max) i = (nextProd, max prod nextProd) 
         ^^^ 
在該範圍

maxInt(超載max功能)。

+0

arghhh ...我一定會變得失明:[|]謝謝! – Julio 2015-02-11 11:30:10

+3

@Julio使用'-Wall' GHC標誌會觸發全局'max'的陰影警告,指出問題所在。 – chi 2015-02-11 11:59:17