I've started experimenting with Haskell and have a problem. qqq is a function that should print one string if called with "Nothing" and print other things if called with "Just something".
The first attempt seems like working:
qqq Nothing = print "There isn't anything to be printed."
qqq (Just x) = print "There is something to be printed." >> print x
main :: IO ()
main = qqq (Just 43)
But:
- when I try to make
main = qqq (Nothing)
it fails ("Ambiguous type variable `a0' in the constraint: (Show a0) arising from a use of 'qqq'") - When I want to add type signature if fails:
qqq :: Maybe x => x -> IO ()
->Type constructor 'Maybe' used as a class
-> But isn't it?
qqq :: (Maybe x) -> IO ()
. Now the signature itself looks like succeed. Butmain = qqq (Just 43)
starts failing with that mysterious(Show a0)
error like inmain = qqq (Nothing)
case.
Questions:
- Why calling qqq with
Nothing
is so different than calling withJust 43
? - What is
(Show a0)
? It is mentioned only in error messages. Any attempts to use it lead to something like "Show not in the scope". - What is correct type signature for this? How to make Haskell print type signature it deduced? Expecting something like:
f 0 = 2
f x = (f (x-1)) + 3
main = print get_type_as_string(f)
-- prints "Number -> Number"