在我为函数式编程课程所做的练习中,系统要求我找到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,请返回模的乘积。
我遇到的错误很奇怪,因为看来我没有匹配任何类型。
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)
答案 0 :(得分:5)
将函数f
应用于参数x
的语法是f x
,而不是f(x)
。应用程序向左关联,因此f x y
的意思是(f x) y
,即将f
应用于x
,并将结果函数应用于y
。如果x
本身是一个包含函数应用程序的复杂表达式,则可以使用多余的括号来消除歧义;因此,例如:
a b c d
将函数a
应用于参数b
,c
和d
a (b c) d
将函数a
应用于参数b c
和d
a b (c d)
将函数a
应用于参数b
和c d
a (b c d)
将函数a
应用于单个参数b c d