Your first program

Contents
The file
Run it
Compile it
The layout rule

The file

A Haskell program is a module with a function called main. Create a file named Main.hs and put two lines in it:

main :: IO () main = putStrLn "Hello, world!"

The signature says that main performs input and output and returns nothing interesting, which the unit type () expresses. The second line is the body: call putStrLn with one string.

Run it

runghc interprets the file without producing an executable. It is the quick loop for experiments:

runghc Main.hs

Hello, world!

Compile it

ghc produces a native binary. The default output name drops the extension, so the program is called main:

ghc Main.hs

[1 of 1] Compiling Main ( Main.hs, Main.o ) Linking main ...

./main

Hello, world!

ghc -O2 Main.hs enables optimisations, and ghc -o hello Main.hs chooses the binary name. Compilation artefacts land in the current directory, which is why real projects use Cabal instead.

The layout rule

Haskell has no braces for a block. Indentation defines it: lines that start in the same column belong together. A do block lines up its statements:

main = do putStrLn "What is your name?" name <- getLine putStrLn ("Hello, " ++ name)

Mixing tabs and spaces breaks the rule in ways the error message rarely explains. Configure the editor to insert four spaces for a tab in .hs files and the problem disappears.

Next: Values with let and where

Article author: Arthur Isaev

Related articles
Haskell
Haskell course: zero to hero in 50 lessons
What is Haskell and why it matters
Install the toolchain with GHCup
GHCi: the interactive shell
Your first program
Values with let and where
Types and type signatures
Functions and application
Currying and partial application
Operators, sections, and fixity
if, guards, and pattern guards
case expressions
Pattern matching
Tuples
Lists
Ranges and enumeration
List comprehensions
Recursion replaces loops
map, filter, and foldr
Lambdas and composition
Type classes
Parametric polymorphism
Maybe and null safety
Either and recoverable errors
data: product types
data: sum types
Records
newtype and type synonyms
Recursive data: binary trees
Functor
Applicative
Monad
do notation
IO and purity
Files and IO
Modules and imports
Cabal projects
Dependencies
Testing with HUnit
Property testing with QuickCheck
Laziness and infinite lists
Strictness and space leaks
Folds
Monoids
Traversable
Monad transformers
Errors and exceptions
Concurrency
Parsing with Megaparsec
Capstone: a todo CLI
Hero roadmap: the whole course on one page
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: