Type of return in do block

Viewed 4624

I am trying to understand Monads in Haskell and during my countless experiments with code I have encountered this thing:

f2 = do   
  return "da"

and the fact that it doesnt want to compile with huge error regarding type. I think the only important part is this:

 No instance for (Monad m0) arising from a use of return'
The type variable `m0' is ambiguous  

So then I have changed my code to:

 f2 = do   
 return "da"  :: IO [Char]  

And it worked perfectly well. But when I have tried to mess up a bit and change the type to IO Int it was an error once again. So why the type is "ambiguous" when it actually isnt? Also when I will add something before return like:

f2 = do   
  putStrLn "das"  
  return 2

Then I dont have to specify the type of return. So can someone explain me what is going on really? Also why is return outputting "da" in the first case? Not da without ""?

3 Answers
Related