Home > Blockchain >  What can be the fault in this haskell code?
What can be the fault in this haskell code?

Time:10-08

Friendly numbers

  • Pairs of numbers for which it is true that the sum of the divisors of one number less than itself is equal to another number and vice versa, we call it friendly numbers. An example is the number pair (220; 284).

  • 220: smaller divisors: 1 2 4 5 10 11 20 22 44 55 110 = 284

  • 284: smaller divisors: 1 2 4 71 142 = 220

  • Enter the areAmicableNumbers function, which decides a pair of numbers if they are friendly numbers!

  • Each of the following test cases must give a 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.

CodePudding user response:

1==1 has type Bool.

[1==1] has type [Bool].

(&&) expects two arguments of type Bool. Instead it finds two arguments of type [Bool]. the two types are different. The program is thus rejected.

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]

The square brackets around your tests are wrong. They shouldn't be there.

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
  • Related