1

中国剰余定理アルゴリズムを機能させようとしているので、オンラインでヘルプを探しています。Haskell でこの CRT の例をコンパイルしようとしていますが、これらのエラーが発生しています。extGCD独自の機能を実装しました。

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.
4

1 に答える 1

4

This isn't Python. Lists and tuples are not the same type. Nor even close.

That's what the error message is telling you. (Integer, Integer, Integer) isn't a list. So you can't apply the !! operator to the return from extGCD.

The base package doesn't include functions for working with triples, so I'd probably change the list comprehension to something like [ x' | x <- ms, let (_, x', _) = extGCD x (div p x) ].

于 2014-02-25T05:00:06.173 に答える