I'm learning Haskell and am trying to define a function that takes two numbers, add them and then mod 3. I can do it in the following way:
Prelude> let g :: Int -> Int -> Int; g = (\x y -> mod (x+y) 3)
But when I try to write it as a composition of two functions, I'm getting an error and am having a hard time interpreting the error message:
Prelude> let g :: Int -> Int -> Int; g = (\x -> mod x 3) . (\x y -> x + y)
<interactive>:72:40:
No instance for (Integral (Int -> Int))
(maybe you haven't applied enough arguments to a function?)
arising from a use of ‘mod’
In the expression: mod x 3
In the first argument of ‘(.)’, namely ‘(\ x -> mod x 3)’
In the expression: (\ x -> mod x 3) . (\ x y -> x + y)
<interactive>:72:46:
No instance for (Num (Int -> Int))
(maybe you haven't applied enough arguments to a function?)
arising from the literal ‘3’
In the second argument of ‘mod’, namely ‘3’
In the expression: mod x 3
In the first argument of ‘(.)’, namely ‘(\ x -> mod x 3)’
What's wrong here? Can't we compose like this?