Printing out the type of something in IO operations in Haskell

Viewed 47

I have a block of code

main = do
   args <- getArgs --args is a list.
   ...
   let finalMappedContents = addUpSameWord mappedContents 

For the final IO statement, I want to print out the type of finalMappedContents. How can I do this? I know of the typeOf function, but I'm not completely sure how to use that here.

1 Answers

You can use showsTypeRep in order to get the String representation of the type:

λ> let foo = "Foo"
λ> import Data.Typeable
λ> putStrLn $ showsTypeRep (typeOf foo) ""
[Char]

In your case it would be:

putStrLn $ showsTypeRep (typeOf finalMappedContents) ""

Edit

Thanks to @dfeuer for pointing out that you can also just use the Show instance:

print $ typeOf finalMappedContents
Related