这个haskell代码有什么问题?

马罗斯

友好号码

  • 一个数的除数之和小于它自己的数与另一个数相等,反之亦然的一对数,我们称之为友好数。一个例子是数字对 (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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章