Search code examples
haskellpredicateboolean-expressionmodulo

Couldn't match expected type ‘Integer -> t’ with actual type ‘Bool’


In Haskell,

This works perfectly fine:(mod 9) 7. It gives the expected result: remainder when 9 is divided by 7 (2).

Similarly, this works too:(mod 9) 9. It returns 0.

This led me to think that (mod 9 == 0) 9 should return True. However, that hasn't been the case: it threw up an error instead.

THE ERROR:

<interactive>:62:1: error:
    • Couldn't match expected type ‘Integer -> t’
                  with actual type ‘Bool’
    • The function ‘mod 9 == 0’ is applied to one argument,
      but its type ‘Bool’ has none
      In the expression: (mod 9 == 0) 9
      In an equation for ‘it’: it = (mod 9 == 0) 9
    • Relevant bindings include it :: t (bound at <interactive>:62:1)

Please help me understand why (mod 9 == 0) 9 wouldn't return True.

P.S.: I'm convinced that my usage of "return" in Haskell's context is flawed. However, I am just starting out, so please excuse me. (Would be nice if you could correct me if I am, indeed, wrong.)


Solution

  • As I mentioned in a comment, it appears that you expect mod 9 == 0 to be a function that takes an argument, passes it to mod 9, then returns the result of the comparison. You can write such an expression, but it's a little more complicated.

    >>> ((== 0) . (mod 9)) 9
    True
    

    Here, (== 0) . (mod 9) is the composition of two functions, (== 0) and mod 9. The composed function takes its argument, applies mod 9 to it, then applies (== 0) to the result. (Where (== 0) is a short form for \x -> x == 0.)