无法匹配 Haskell 中的整数类型

Couldn't match type Integer in Haskell

在我为函数式编程课程做的练习中,我被要求找到 x mod a = b 的最低 x,给出一系列对 (a, b).

在给定三对(元组)的情况下,我使用以下代码:

solveModularEq :: [(Integer,Integer)] -> Integer
solveModularEq [(a),(b),(c)] = lowestModThree(fst(a) snd(a) fst(b) snd(b) fst(c) snd(c) 1)

lowestModThree :: Integer -> Integer -> Integer -> Integer -> Integer -> 
Integer -> Integer -> Integer 
lowestModThree a b c aa bb cc k
  | k `mod` a == aa && k `mod` b == bb && k `mod` c == cc = k
  | k > (aa * bb * cc) = aa * bb * cc
  | otherwise = lowestModThree a b c aa bb cc (k+1)

在没有x的情况下,return模数的乘积。

我得到的错误很奇怪,因为我似乎没有匹配任何类型。

modEq.hs:3:32:
   Couldn't match expected type ‘Integer’
              with actual type ‘Integer
                                 -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer’
   Probable cause: ‘lowestModThree’ is applied to too few arguments
  In the expression:
     lowestModThree (fst (a) snd (a) fst (b) snd (b) fst (c) snd (c) 1)
   In an equation for ‘solveModularEq’:
      solveModularEq [(a), (b), (c)]
         = lowestModThree
             (fst (a) snd (a) fst (b) snd (b) fst (c) snd (c) 1)

modEq.hs:3:51:
  Couldn't match type ‘Integer’
                  with ‘((a0, b0) -> b0)
                       -> (Integer, Integer)
                        -> ((a1, b1) -> a1)
                        -> (Integer, Integer)
                        -> ((a2, b2) -> b2)
                        -> (Integer, Integer)
                        -> ((a3, b3) -> a3)
                        -> (Integer, Integer)
                        -> ((a4, b4) -> b4)
                        -> (Integer, Integer)
                        -> Integer
                        -> Integer’
   Expected type: (((a0, b0) -> b0)
                   -> (Integer, Integer)
                   -> ((a1, b1) -> a1)
                   -> (Integer, Integer)
                   -> ((a2, b2) -> b2)
                   -> (Integer, Integer)
                   -> ((a3, b3) -> a3)
                   -> (Integer, Integer)
                   -> ((a4, b4) -> b4)
                   -> (Integer, Integer)
                   -> Integer
                   -> Integer,
                   Integer)
     Actual type: (Integer, Integer)
   In the first argument of ‘fst’, namely ‘(a)’
   In the first argument of ‘lowestModThree’, namely
     ‘(fst (a) snd (a) fst (b) snd (b) fst (c) snd (c) 1)’

同样的情况发生在我的递归素数测试实现的实现中。

isPrimeRec :: Int -> Bool
isPrimeRec n = isPrimeRec'(isqrt(n) n)

isPrimeRec' :: Int -> Int -> Bool
isPrimeRec' divisor n
  | mod n divisor == 0 = isPrimeRec' (divisor-1) n
  | mod n divisor /= 0 = False
  | divisor < 2 = True

这个错误是

palPrimes.hs:10:16:
    Couldn't match expected type ‘Bool’ with actual type ‘Int -> Bool’
    Probable cause: ‘isPrimeRec'’ is applied to too few arguments
    In the expression: isPrimeRec' (isqrt (n) n)
    In an equation for ‘isPrimeRec’:
        isPrimeRec n = isPrimeRec' (isqrt (n) n)

palPrimes.hs:10:28:
    Couldn't match expected type ‘Int -> Int’ with actual type ‘Int’
    The function ‘isqrt’ is applied to two arguments,
    but its type ‘Int -> Int’ has only one
    In the first argument of ‘isPrimeRec'’, namely ‘(isqrt (n) n)’
    In the expression: isPrimeRec' (isqrt (n) n)

将函数 f 应用于参数 x 的语法是 f x,而不是 f(x)。应用程序向左关联,因此 f x y 表示 (f x) y —— 即,将 f 应用于 x,并将结果函数应用于 y。如果x本身是一个复杂的表达式,里面有函数的应用,可以使用额外的括号来消除歧义;所以,例如:

  • a b c d 将函数 a 应用于参数 bcd
  • a (b c) d 将函数 a 应用于参数 b cd
  • a b (c d) 将函数 a 应用于参数 bc d
  • a (b c d) 将函数 a 应用于单个参数,b c d