40

How do I print a list to stdout in Haskell?

Let's say I have a list [1,2,3] and I want to convert that list into a string and print it out. I guess I could build my own function, but surely Haskell has a function built in to do that.

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
Alexander Bird
  • 38,679
  • 42
  • 124
  • 159

1 Answers1

55

Indeed there is a built in function, aptly named print.

> print [1,2,3]
[1,2,3]

This is equivalent to putStrLn $ show [1,2,3].

hammar
  • 138,522
  • 17
  • 304
  • 385
  • 1
    Following up on what @Dan said, anything that implements the Show TypeClass has a default printable representation. – Daniel May 11 '11 at 12:41
  • 1
    How would I do it in case the list was not of standard type like Int but of custom type (Eg: type Height = Int) – Bikash Gyawali May 22 '11 at 09:03
  • 1
    @Bikash: As long as the contained type is an instance of `Show`, it will work fine. `type Height = Int` does not create a custom type, only an alias, so it behaves exactly as if you'd used `Int`s. – hammar May 22 '11 at 09:27