1
我試圖讓中國剩餘定理算法運行起來,所以我一直在網上尋找幫助。我試圖在haskell中得到這個CRT的例子來編譯,但是我收到了這些錯誤。我已經實現了我自己的extGCD
函數。Haskell中的CRT實現
extGCD :: Integer -> Integer -> (Integer, Integer, Integer)
extGCD a 0 = (a, 1, 0)
extGCD a b = let (q, r) = divMod a b
(d, m, n) = extGCD b r
in (d, n, m - n*q)
crt :: [Integer] -> [Integer] -> Integer
crt as ms = let {p = product ms
;ls = [extGCD x (div p x) !! 1 |x<- ms]
}in sum [ div (x*y*p) z | (x,y,z)<- zip3 as ls ms ]
這裏的錯誤:
Couldn't match expected type `[t0]'
with actual type `(Integer, Integer, Integer)'
In the return type of a call of `extGCD'
In the first argument of `(!!)', namely `extGCD x (div p x)'
In the expression: extGCD x (div p x) !! 1
Failed, modules loaded: none.
哦,這是有道理的!我覺得有點傻哈哈。感謝您的快速回復,我會看看我現在是否可以開展工作。 – user3349534