75

I am confused. I tried to use print, but I know people apply putStrLn. What are the real differences between them?

print $ function 
putStrLn $ function
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Amir Nabaei
  • 1,582
  • 1
  • 15
  • 26
  • 37
    `print = putStrLn . show` – Satvik Oct 10 '13 at 07:04
  • 6
    Have you tried running `print 3`, `putStrLn 3`, `print "three"`, `putStrLn "three"` in ghci? This is really a question that can be answered by a bit of experimentation. – Jonathan Cast Apr 13 '16 at 17:24
  • Unasked plug: defining `proom = putStrLn . groom` (from the `groom` package) increases AST readability a lot. No more hunting for "where does the next element start". – ron Aug 15 '23 at 11:24

1 Answers1

116

The function putStrLn takes a String and displays it to the screen, followed by a newline character (put a String followed by a new Line).

Because it only works with Strings, a common idiom is to take any object, convert it to a String, and then apply putStrLn to display it. The generic way to convert an object to a String is with the show function, so your code would end up with a lot of

putStrLn (show 1)
putStrLn (show [1, 2, 3])
putStrLn (show (Just 42))

Once you notice that, it's not a very big stretch to define a function that converts to a String and displays the string in one step

print x = putStrLn (show x)

which is exactly what the print function is.

MasterMastic
  • 20,711
  • 12
  • 68
  • 90
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
  • 1
    `putStrLn` can show non ASCII characters like "я" whereas `print` cannot. I don't really know why though. Try `putStrLn "я"` vs `print "я"`. – CMCDragonkai May 24 '15 at 09:07
  • 3
    @CMCDragonkai It's for the reason I give in the answer. The `print` function calls `putStrLn` on the output of `show`, and the `show` functions converts strings to their unicode representation in order to display them. The unicode point for 'я' (Cyrillic letter "ya") is U+044F, or 1103 in decimal, which is why `show "я"` outputs `"\"\\1103\""` - this is what you would have to type in ghci to get the string composed of the seven characters `"\1103"` (try it!) – Chris Taylor May 24 '15 at 12:58
  • Doesn't this mean it would better to use `putStrLn` when working with text in general? – CMCDragonkai May 24 '15 at 13:04
  • 8
    If you have a `String` that you want to print to the screen, you should use `putStrLn`. If you have something *other than* a `String` that you want to print, you should use `print`. Look at the types! `putStrLn :: String -> IO ()` and `print :: Show a => a -> IO ()`. – Chris Taylor May 24 '15 at 13:17
  • 4
    You seem to be a bit confused. Literally the only difference between `putStrLn` and `print` is that `print` calls `show` on its input first. Any difference between the result is because you have called `show` on the input in one case, and not in the other. So when choosing which one to use, ask yourself - do I want to call `show` on the input or not? If the input is a `String` then you almost certainly *do not* want to call `show` on it first. – Chris Taylor May 24 '15 at 13:18