Your first program
The file
A Haskell program is a text file whose definitions include one called main. Save this as Hello.hs:
module Main (main) where
main :: IO ()
main = putStrLn "Hello, world!"
Four lines, one of them empty, and the program is complete. The rest of the lesson reads them one at a time.
Build it and run it
ghc -Wall -O1 -o hello Hello.hs
./hello
echo "status: $?"
[1 of 2] Compiling Main ( Hello.hs, Hello.o )
[2 of 2] Linking hello
Hello, world!
status: 0
The two bracketed lines go to stderr, the greeting goes to stdout, and the shell mixes both in a terminal. That is why the build lines can be missing and the hello still print: the binary has been built before. Delete hello or change the source to see the compiler work again.
What each line does
module Main (main) where
Every file is a module. The line names it and, in brackets, the things it
exports; everything else stays private to the file. Here the module is
Main and it exports one name,
main, which is the name the runtime looks for. A file
whose module is Main is the entry point of a program.
The export list is optional. Without it, the module exports everything it
defines, which is fine for a small program and bad for a library: the
lesson on modules uses the
list to keep a detail private.
main :: IO ()
This is the type signature: main takes no arguments
and is an I/O action that produces nothing of interest,
(). The type is the promise. A program whose
main promised something else would be rejected,
because the runtime knows what to do with an IO ()
and nothing else.
The signature is not required, and -Wall warns when
it is missing. Write it anyway: it is the first thing a reader looks at, and
it is checked against the definition, so it cannot lie.
main = putStrLn "Hello, world!"
= defines a value, not an assignment: the left side
is the name, the right side is the expression whose value it has.
putStrLn takes a string and returns an I/O action
that prints it with a newline when it runs. Since
main is that action, the runtime runs it at startup.
There is no call to main anywhere in the file. A
program is a definition of an action, and the runtime is the caller.
putStr, putStrLn and show
A program that prints several things needs a do block, which runs the actions in order. This one also shows the difference between the two printing functions:
module Main (main) where
main :: IO ()
main = do
putStr "one "
putStr "two"
putStrLn ""
putStrLn ("2 + 3 = " ++ show (2 + 3 :: Int))
ghc -Wall -O1 -o chatty Chatty.hs
./chatty
[1 of 2] Compiling Main ( Chatty.hs, Chatty.o )
[2 of 2] Linking chatty
one two
2 + 3 = 5
- putStr prints a string and adds nothing.
- putStrLn prints a string and a newline. The empty string still gives a newline, which is how the first line ends after "one two".
- show turns a value into the string that print would show. print is putStrLn . show in one function.
The annotation (2 + 3 :: Int) is there because 2 and 3 have no type of their own: any numeric type would do, and -Wall warns when the compiler has to pick one. The lesson on numbers is about that choice; for now, the annotation says "the machine integer".
A clean build
Both programs above were built with -Wall and printed no warning, which is the state a file should be in before it is committed. The other two checks this course runs on every program say the same:
ghc -fno-code -Wall -O1 Hello.hs
hlint Hello.hs
[1 of 1] Compiling Main ( Hello.hs, nothing )
No hints
-fno-code type-checks and writes nothing; hlint reads the source and suggests a simpler way to write it, and says No hints when it has none. Neither is required to run a program, and both are cheap enough to run on every save. The next lesson is about the messages you get when they are not clean.
Exercises
- Change the greeting and rebuild. What happens if you forget to rebuild and run the old binary?
- Remove the signature main :: IO () and build with -Wall. Which warning appears, and does the program still run?
- Add putStrLn "second line" under the first one without a do. Read the error, then add the do.
- Print 42 with print and then with putStrLn (show (42 :: Int)). Are the two outputs the same?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which name does the runtime call when a Haskell program starts?
Answer
main
The runtime looks for main, whose type must be an I/O action, normally IO ().
-
What does the module line
module Main (main) wheresay?Answer
The module is named Main and exports only main
The line names the module and gives its export list, here the single name main.
-
What does the type
IO ()of main mean?Answer
main is an I/O action whose result is the empty value
IO marks an action that may touch the outside world, and () is the empty value it produces when it is done.
-
Which function prints a string and a newline?
Answer
putStrLn
putStrLn adds the newline; putStr does not, print adds one after showing the value, and show only builds a string.
-
Why is
printcalledputStrLn . showin one function?Answer
Because it shows a value and prints the result with a newline
print turns the value into its textual form and prints that string with a newline, which is exactly the composition of show and putStrLn.
-
What does this program print?
module Main (main) where main :: IO () main = putStrLn "Hello, world!"Answer
Hello, world!
putStrLn prints the string without its quotes and adds one newline.
-
What does this program print?
module Main (main) where main :: IO () main = do putStr "one " putStr "two" putStrLn ""Answer
one two
putStr writes without a newline, so both words share a line, and the empty putStrLn ends it.
-
What does this program print?
module Main (main) where main :: IO () main = print (2 + 3 :: Int)Answer
5
The expression is evaluated to 5 and print shows the number, without quotes.
-
A file starts with
module Main (main) whereand defines a second top-level name that nothing uses. What does the build with -Wall report?Answer
A warning about an unused top-level binding
-Wunused-top-binds is part of -Wall, so a definition that nothing uses is reported as a warning.
-
What is the first line of the compiler output for this program?
module Main (main) where main :: IO () main = do putStrLn "first" putStrLn 42Answer
error: [GHC-39999]
putStrLn needs a String and 42 is a number, so no Num String instance exists, which GHC reports as GHC-39999.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev