TExcelTAD
Home › Haskell › Haskell First Program

Haskell First Program

Every runnable Haskell program needs a value called main of type IO (). This lesson takes a small program apart line by line and introduces the basic output functions, comments, do blocks and layout.

haskell · try it yourself
-- A tiny greeting program
module Main where

main :: IO ()
main = do
  putStrLn "Welcome to ExcelTAD!"
  putStr "Learning: "
  putStrLn "Haskell"
  print 2026
Welcome to ExcelTAD!
Learning: Haskell
2026
Line Meaning
-- A tiny greeting program A comment; everything after -- on the line is ignored
module Main where Names the module. Optional for the main file (it is Main by default)
main :: IO () A type signature: main is an I/O action that produces no useful result ((), "unit")
main = do … The definition. do glues several actions into one, run top to bottom

Output functions

Function Type What it does
putStrLn String -> IO () Writes the text and a newline
putStr String -> IO () Writes the text without a newline
print Show a => a -> IO () Converts any showable value with show, then writes it with a newline
show Show a => a -> String Turns a value into its text representation (a pure function, prints nothing)

putStrLn only accepts a String. To print a number in a sentence, convert it with show and join the strings with ++:

haskell · try it yourself
main :: IO ()
main = do
  let lessons = 44
      hours = 2.5
  putStrLn ("This course has " ++ show lessons ++ " lessons.")
  putStrLn ("About " ++ show hours ++ " hours per week.")
  print "quoted"
  putStrLn "not quoted"
  print [1, 2, 3]
  print (lessons, True)
This course has 44 lessons.
About 2.5 hours per week.
"quoted"
not quoted
[1,2,3]
(44,True)

let inside a do block defines local names. The names are not variables: once lessons is 44 it stays 44.

Comments

haskell · try it yourself
{- A block comment can span
   several lines. {- They can even be nested. -} -}

-- | A comment that starts with "-- |" documents the next definition (Haddock).
greeting :: String
greeting = "Hi" -- end-of-line comment

main :: IO ()
main = putStrLn greeting
Hi

Layout: indentation matters

Haskell uses layout (indentation) instead of braces and semicolons. All the actions in a do block must start in the same column. If one line is indented differently, GHC misreads the structure:

haskell · try it yourself
main :: IO ()
main = do
  putStrLn "first"
   putStrLn "second"
<source>:3:3: error: [GHC-83865]
    * Couldn't match expected type: (String -> IO ())
                                    -> String -> IO ()
                  with actual type: IO ()
    * The function `putStrLn' is applied to three visible arguments,
        but its type `String -> IO ()' has only one
      In a stmt of a 'do' block: putStrLn "first" putStrLn "second"
      In the expression: do putStrLn "first" putStrLn "second"
  |
3 |   putStrLn "first"
  |   ^^^^^^^^^^^^^^^^...

Because the second line is indented more, it was read as a continuation of the first line, so putStrLn seemed to get three arguments. Line the statements up and the error disappears.

Tip: Use spaces, not tabs, and configure your editor to indent by two or four spaces. GHC warns about tabs.

Running actions in order

A do block runs its actions in order, so it reads like a script. mapM_ runs an action for every item of a list, which is the usual replacement for a for loop that prints:

haskell · try it yourself
main :: IO ()
main = do
  putStrLn "Countdown:"
  mapM_ print [3, 2, 1]
  putStrLn "Lift-off!"
  mapM_ putStrLn ["- red", "- green", "- blue"]
Countdown:
3
2
1
Lift-off!
- red
- green
- blue

What about input?

Reading input uses getLine :: IO String, and you bind its result with <-:

haskell
main :: IO ()
main = do
  putStrLn "What is your name?"
  name <- getLine
  putStrLn ("Nice to meet you, " ++ name ++ "!")

The playgrounds have no keyboard input, so this course passes data in as values instead. You will learn exactly what <- does in the do-notation lesson.

Test yourself

What is the type of main in a Haskell program?

  1. String
  2. IO ()
  3. Int
  4. main -> ()

Which line prints Score: 10?

  1. putStrLn "Score: " ++ 10
  2. putStrLn ("Score: " ++ show 10)
  3. print ("Score: " ++ 10)
  4. putStrLn ("Score: " + show 10)

What does print "ok" output?

  1. ok
  2. "ok" (with quotes)
  3. Nothing
  4. A type error