Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 57 ยท Getting started

Your First Haskell Program

Write and compile a Haskell program: main, putStrLn, do blocks, the module header, comments and the layout rule that uses indentation instead of braces.

Every executable Haskell program has an entry point named main. In this lesson you write one, learn what the do block is for, see how comments and modules look, and meet the layout rule: Haskell uses indentation to group code, a bit like Python.

The Haskell code below is real and read-only; you can run it with runghc once you have installed GHC. The runnable examples are JavaScript stand-ins that imitate the ideas.

Hello, world

Save this as Hello.hs:

HASKELL
main :: IO ()
main = putStrLn "Hello, Haskell!"

Run it with runghc Hello.hs, or compile it with ghc Hello.hs and start ./Hello. It prints:

TEXT
Hello, Haskell!

Let us read it piece by piece:

  • main :: IO () is the type signature. It says main is an IO action that produces (), called "unit", the type with only one value and no information. Think of it as "does something with the outside world, returns nothing useful".
  • main = putStrLn "Hello, Haskell!" is the definition. putStrLn takes a String and returns an IO action that prints it followed by a newline. Use putStr for no newline.
  • There are no parentheses around the argument. putStrLn "text" is a function call.

Several actions with do

A do block glues several IO actions into one, running them from top to bottom:

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

Three things are new here:

  1. do starts a block of steps.
  2. name <- getLine runs getLine, which reads a line of input, and binds the result to the name name. Use <- for results of IO actions.
  3. ++ joins two strings. The parentheses are needed because function application binds tighter than ++; without them, putStrLn would receive only the first piece.

If you type Ada, the output is:

TEXT
What is your name?
Nice to meet you, Ada!

You will use let inside do to name ordinary values (no IO involved):

HASKELL
main :: IO ()
main = do
  let greeting = "Hello"
      target   = "world"
  putStrLn (greeting ++ ", " ++ target)

Notice that let here has no in, and greeting and target start in the same column. That brings us to layout.

The layout rule

Haskell does not need braces and semicolons around blocks. Instead, the compiler looks at the column where the first item after do, let, where or of starts. Every following line that starts in the same column is a new item of that block; a line indented further continues the previous item; a line indented less ends the block.

Three indentation outcomes

Same column A new statement in the same block.
Further right The previous statement continues.
Further left The block ends.

Use spaces, never tabs.

1 / 2
HASKELL
main :: IO ()
main = do
  putStrLn "one"      -- same column: separate statements
  putStrLn
    "two"             -- further right: continues the line above
  putStrLn "three"

All three lines print. If you accidentally indent a statement by a different amount, you get a parse error such as parse error on input. Use spaces only, since tabs can be counted differently by different editors. Two or four spaces are both common; two is typical in Haskell code.

Explicit braces and semicolons do exist (do { a; b }) and are legal, but almost nobody writes them.

Comments

HASKELL
-- A line comment runs to the end of the line.

 A block comment
   can span several lines. 

 Block comments  can nest  inside each other. -}

-- | A Haddock comment documents the thing that follows it.
square :: Int -> Int
square x = x * x

-- | is the style used by Haddock, the documentation tool. Two dashes start a comment only when they are not part of a longer operator: --> is an operator, not a comment.

Modules

A source file is a module. The header names it and lists what it exports. For a program, the main module is called Main, and it must define main:

HASKELL
module Main (main) where

import Data.Char (toUpper)

main :: IO ()
main = putStrLn (map toUpper "quiet")

module Main (main) where exports only main. If you leave the header out, GHC assumes module Main (main) where for you, which is why the tiny examples above work. import Data.Char (toUpper) brings one function from the standard library into scope. The output of this program is QUIET. Modules are covered fully in modules and imports.

Compiling

Terminal
ghc -Wall Hello.hs
./Hello

-Wall turns on most warnings, such as unused variables and missing type signatures. Keep it on from the start; the warnings are mostly good advice.

A JavaScript stand-in for main and do

JavaScript has no IO type, so this stand-in models an action as a function that you run later, and doBlock runs a list of actions in order. That is the idea of do, not the real mechanism.

JS Actions as values

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that nothing appears until main() is called. In Haskell, too, building an IO action does nothing by itself; only the runtime running main performs it.

JS Layout as nested structure

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the type IO () mean for main?

    IO () is an action that may interact with the outside world and returns (), meaning no useful result.
  2. 2. In a do block, which line reads a line of input into name?

    <- runs an IO action and binds its result. Plain = would just give name the action itself.
  3. 3. What does a line indented less than the rest of a do block do?

    Layout compares columns. A smaller indentation closes the block.
  4. 4. Why does putStrLn ("Hi, " ++ name) need parentheses?

    putStrLn "Hi, " ++ name would parse as (putStrLn "Hi, ") ++ name, which is a type error.

Summary

  • main :: IO () is the entry point; putStrLn prints a line.
  • A do block runs IO actions in order; x <- action binds a result and let names a plain value.
  • Layout uses indentation: same column means a new item, deeper means continuation, shallower ends the block. Use spaces.
  • Comments are -- and ; a file is a module and module Main (main) where is the default header.
  • Compile with ghc -Wall and run with runghc while learning.

Next: Expressions, Functions and Application.