Reputation: 4581
I have a very simple function called 'fold' that takes a function f and executes it. Given a function 'add' and two numbers it calls add with those two numbers and displays the results. However I cannot compile it due to a compilation error below. What is the error specifically stating and what can I do to solve it?
module Main where
add x y = x + y
--fold :: ((a,b)->c) -> a->b->c
fold f n x = f n x
main :: IO ()
main = do
fold add 2 3
Upvotes: 0
Views: 101
Reputation: 532333
The problem is that you've declared main
(correctly) as having type IO ()
, but fold
doesn't return that. The error message is a little more complicated because in theory, add
(and thus fold
) could return a value of type IO ()
, since add
can return any type with a Num
instance. IO ()
, however, is not an instance of Num
. The solution is to return an IO action created from the return value of fold
. One way to do that is to use the print
function (which takes any value with a Show
instance and converts it to a String
before outputing it).
main = print $ fold add 2 3
Upvotes: 6