🎓 A1Learner
🔍
0 of 54 lessons completed

Haskell Output

Output is the text and values a program shows when it runs. Haskell has three common actions for showing output, and each one formats its result in its own way.

putStrLn prints text on its own line

putStrLn prints the text inside the quotes. It adds a newline after the text.

main :: IO ()
main = putStrLn "Hello"

putStr prints text with no newline

putStr prints the text exactly as written. Nothing extra follows it.

main :: IO ()
main = putStr "Loading"

print wraps text in quotes

print shows the Show form of a value. For text, that form includes the double quotes.

main :: IO ()
main = print "Hello"

Example

main :: IO ()
main = print (6 * 7)

Key points

  • putStrLn: prints text and then adds a newline at the end.
  • putStr: prints text with no newline at the end.
  • print: prints a value in its standard written form and adds a newline.
  • Show form: the standard written form of a value, the way print displays it.
  • Text in Show form: print shows text wrapped in double quotes.

In this example, print works out 6 * 7 first. It then shows the result, 42, in its Show form, with a newline after it.

Note

putStrLn and putStr take text only, while print takes any value that has a Show form.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Haskell Playground

Exercise

What does print "cat" show?