这个 haskell 代码有什么问题?
What can be the fault in this haskell code?
友情号码
一对数,其中一个数小于它自身的约数之和等于另一个数,反之亦然,我们称之为友好数。一个例子是数字对 (220; 284).
220:较小的除数:1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284
284:较小的除数:1 + 2 + 4 + 71 + 142 = 220
进入areAmicableNumbers
函数,判断一对数字是否友好!
下面每个测试用例都必须给出一个True
:
areAmicableNumbers 220 284 == True
not (areAmicableNumbers 220 283) == True
areAmicableNumbers 1184 1210 == True
areAmicableNumbers :: Integral a => a -> a -> [Bool]
areAmicableNumbers x y =
[(sum [k | k <- [1..x], x `mod` k ==0]) == y]
&&
[(sum [i | i <- [1..y], y `mod` i ==0]) == x]
Error: Homework1.hs:23:26: error:
* Couldn't match expected type `Bool' with actual type `[Bool]'
* In the first argument of `(&&)', namely
`[(sum [k | k <- [1 .. x], x `mod` k == 0]) == y]'
In the expression:
[(sum [k | k <- [1 .. x], x `mod` k == 0]) == y]
&& [(sum [i | i <- [1 .. y], y `mod` i == 0]) == x]
In an equation for `areAmicableNumbers':
areAmicableNumbers x y
= [(sum [k | k <- [1 .. x], x `mod` k == 0]) == y]
&& [(sum [i | i <- [1 .. y], y `mod` i == 0]) == x]
|
23 | areAmicableNumbers x y = [(sum [k | k <- [1..x], x `mod` k ==0]) == y] && [(sum [i | i <- [1..y], y `mod` i ==0]) == x]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Homework1.hs:23:75: error:
* Couldn't match expected type `Bool' with actual type `[Bool]'
* In the second argument of `(&&)', namely
`[(sum [i | i <- [1 .. y], y `mod` i == 0]) == x]'
In the expression:
[(sum [k | k <- [1 .. x], x `mod` k == 0]) == y]
&& [(sum [i | i <- [1 .. y], y `mod` i == 0]) == x]
In an equation for `areAmicableNumbers':
areAmicableNumbers x y
= [(sum [k | k <- [1 .. x], x `mod` k == 0]) == y]
&& [(sum [i | i <- [1 .. y], y `mod` i == 0]) == x]
|
23 | areAmicableNumbers x y = [(sum [k | k <- [1..x], x `mod` k ==0]) == y] && [(sum [i | i <- [1..y], y `mod` i ==0]) == x]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.
1==1
的类型为 Bool
.
[1==1]
的类型为 [Bool]
.
(&&)
需要两个 Bool
类型的参数。相反,它会找到两个 [Bool]
类型的参数。这两种类型是不同的。程序因此被否决。
GHCi> :type (&&)
(&&) :: Bool -> Bool -> Bool
> :type (&&) :: [Bool] -> [Bool] -> [Bool]
<interactive>:1:1:
Couldn't match type `Bool' with `[Bool]'
Expected type: [Bool] -> [Bool] -> [Bool]
Actual type: Bool -> Bool -> Bool
In the expression: (&&) :: [Bool] -> [Bool] -> [Bool]
你测试的方括号是错误的。他们不应该在那里。
GHCi> :type [1==1] :: Bool
<interactive>:1:1:
Couldn't match expected type `Bool' with actual type `[Bool]'
In the expression: [1 == 1] :: Bool
GHCi> :type (1==1)
(1==1) :: Bool
友情号码
一对数,其中一个数小于它自身的约数之和等于另一个数,反之亦然,我们称之为友好数。一个例子是数字对 (220; 284).
220:较小的除数:1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284
284:较小的除数:1 + 2 + 4 + 71 + 142 = 220
进入
areAmicableNumbers
函数,判断一对数字是否友好!下面每个测试用例都必须给出一个
True
:areAmicableNumbers 220 284 == True
not (areAmicableNumbers 220 283) == True
areAmicableNumbers 1184 1210 == True
areAmicableNumbers :: Integral a => a -> a -> [Bool]
areAmicableNumbers x y =
[(sum [k | k <- [1..x], x `mod` k ==0]) == y]
&&
[(sum [i | i <- [1..y], y `mod` i ==0]) == x]
Error: Homework1.hs:23:26: error:
* Couldn't match expected type `Bool' with actual type `[Bool]'
* In the first argument of `(&&)', namely
`[(sum [k | k <- [1 .. x], x `mod` k == 0]) == y]'
In the expression:
[(sum [k | k <- [1 .. x], x `mod` k == 0]) == y]
&& [(sum [i | i <- [1 .. y], y `mod` i == 0]) == x]
In an equation for `areAmicableNumbers':
areAmicableNumbers x y
= [(sum [k | k <- [1 .. x], x `mod` k == 0]) == y]
&& [(sum [i | i <- [1 .. y], y `mod` i == 0]) == x]
|
23 | areAmicableNumbers x y = [(sum [k | k <- [1..x], x `mod` k ==0]) == y] && [(sum [i | i <- [1..y], y `mod` i ==0]) == x]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Homework1.hs:23:75: error:
* Couldn't match expected type `Bool' with actual type `[Bool]'
* In the second argument of `(&&)', namely
`[(sum [i | i <- [1 .. y], y `mod` i == 0]) == x]'
In the expression:
[(sum [k | k <- [1 .. x], x `mod` k == 0]) == y]
&& [(sum [i | i <- [1 .. y], y `mod` i == 0]) == x]
In an equation for `areAmicableNumbers':
areAmicableNumbers x y
= [(sum [k | k <- [1 .. x], x `mod` k == 0]) == y]
&& [(sum [i | i <- [1 .. y], y `mod` i == 0]) == x]
|
23 | areAmicableNumbers x y = [(sum [k | k <- [1..x], x `mod` k ==0]) == y] && [(sum [i | i <- [1..y], y `mod` i ==0]) == x]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.
1==1
的类型为 Bool
.
[1==1]
的类型为 [Bool]
.
(&&)
需要两个 Bool
类型的参数。相反,它会找到两个 [Bool]
类型的参数。这两种类型是不同的。程序因此被否决。
GHCi> :type (&&)
(&&) :: Bool -> Bool -> Bool
> :type (&&) :: [Bool] -> [Bool] -> [Bool]
<interactive>:1:1:
Couldn't match type `Bool' with `[Bool]'
Expected type: [Bool] -> [Bool] -> [Bool]
Actual type: Bool -> Bool -> Bool
In the expression: (&&) :: [Bool] -> [Bool] -> [Bool]
你测试的方括号是错误的。他们不应该在那里。
GHCi> :type [1==1] :: Bool
<interactive>:1:1:
Couldn't match expected type `Bool' with actual type `[Bool]'
In the expression: [1 == 1] :: Bool
GHCi> :type (1==1)
(1==1) :: Bool