1

Is pattern matching not allowed for types to be matched as in the following example?

printit :: Int -> IO ()
printit i = print $ "Int i = " ++ show i

printit :: [Char] -> IO ()
printit s = print $ "[char] s = " ++ show s

main :: IO ()
main = do
    printit 2
    printit "two"

1 Answer 1

2

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]
Sign up to request clarification or add additional context in comments.

6 Comments

Note: [char] s = is incorrect, it's really char s =. Implementing a Printable instance for lists, or at least for [Char], would be tricky, I suppose.
@9000 Oh, whoops, thanks for pointing that out! Should be fixed now. It's not tricky, though this approach does require the FlexibleInstances extension. There are more complicated approaches that avoid extensions; e.g. see showList and its history for the core idea.
Well, yes. Just printing lists is perfectly doable. I wonder if it is possible at all to make a Printable that prints "[foo] = ..." if called with an argument of type [foo], without just defining an instance for every supported case (via FlexibleInstances).
@9000 It is possible, yes, and even fairly easy. For example, my one-line printWithType function does it. You can replicate and specialize the core ideas of printWithType to make a leaner typeclass than Typeable, but the basic idea is the same.
@9000 Perhaps the key insight you're missing is that one can write something like instance Foo a => Foo [a] to handle all types of lists in one go (provided the element types implement some type class). I'm not too sure, though; maybe you would like to open a fresh question with the details of what you're wondering?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.