Type classes provide something similar to that:
class Printable a where printit :: a -> IO ()
instance Printable Int where printit i = print $ "Int i = " ++ show i
instance Printable [Char] where printit s = print $ "[char] s = " ++ show s
You probably want putStrLn
instead of print
in both implementations. You may also like the Typeable
class; one could write
printWithType :: (Show a, Typeable a) => a -> IO ()
printWithType v = putStrLn $ show v ++ " :: " ++ show (typeOf v)
...which behaves thus:
> printWithType 3
3 :: Integer
> printWithType "foo"
"foo" :: [Char]