TExcelTAD
Home › Haskell › Haskell Intro

Haskell Intro

Haskell is a general-purpose programming language built around three ideas: it is purely functional, lazy and statically typed. It was designed in 1990 by a committee of researchers who wanted one shared, open language for functional programming, and it is named after the logician Haskell Curry. Today it is used for compilers, financial systems, web back ends, blockchain tooling and anywhere correctness matters more than squeezing out the last microsecond.

Here is a complete Haskell program. Press ▶ Run to compile it with GHC and run it:

haskell · try it yourself
main :: IO ()
main = do
  putStrLn "Hello from Haskell!"
  print (sum [1 .. 100])
Hello from Haskell!
5050

main is the entry point, putStrLn prints a line of text and print prints any value that can be shown. [1 .. 100] is the list of numbers from 1 to 100, and sum adds them up.

The three big ideas

Idea What it means Why it helps
Pure A function's result depends only on its arguments, and calling it changes nothing else Code is easy to test, reuse and reason about
Lazy Values are computed only when they are actually needed Infinite lists, cheap "define now, compute later" pipelines
Statically typed Every expression has a type that the compiler checks before the program runs Whole classes of bugs never reach production

Pure functions

In most languages a function may read a global variable, write to a file or change its arguments. In Haskell an ordinary function cannot do any of that: given the same input it always returns the same output. Variables never change after they are defined. Effects such as printing are still possible, but they are described by values of the special type IO, so you can tell from a function's type whether it touches the outside world.

haskell · try it yourself
double :: Int -> Int
double n = n * 2

main :: IO ()
main = do
  print (double 21)
  print (double (double 5))
  print (map double [1, 2, 3])
42
20
[2,4,6]

double is pure: its type Int -> Int promises that it takes an Int, returns an Int, and does nothing else.

Lazy evaluation

Haskell does not compute a value until something needs it. That makes it perfectly fine to describe an infinite list and then take only the part you want:

haskell · try it yourself
main :: IO ()
main = do
  let naturals = [1 ..]            -- 1, 2, 3, ... forever
      squares = map (^ 2) naturals
  print (take 6 squares)
  print (takeWhile (< 50) squares)
[1,4,9,16,25,36]
[1,4,9,16,25,36,49]

Static types with inference

The compiler checks types before the program runs, but you rarely have to write them: GHC infers them. Writing a type signature for top-level functions is still good style, because it documents intent and gives clearer error messages. If the types don't fit, the program does not compile at all:

haskell · try it yourself
main :: IO ()
main = putStrLn ("Total: " ++ 42)
<source>:2:31: error: [GHC-39999]
    * No instance for `Num [Char]' arising from the literal `42'
    * In the second argument of `(++)', namely `42'
      In the first argument of `putStrLn', namely `("Total: " ++ 42)'
      In the expression: putStrLn ("Total: " ++ 42)
  |
2 | main = putStrLn ("Total: " ++ 42)
  |                               ^^

You cannot glue a number onto a string by accident. The fix is to convert it explicitly with show 42. Don't worry about reading the whole message yet; you will learn to decode GHC errors as the course goes on.

What Haskell is good at

  • Compilers and language tools: pattern matching on tree-shaped data is a natural fit (Pandoc, the document converter, is written in Haskell, as is the Elm compiler).
  • Reliable back ends: strong types make large refactorings safe.
  • Concurrency: immutable data and software transactional memory remove many race conditions.
  • Learning: even if you never ship Haskell, the ideas (map/filter/fold, Maybe instead of null, algebraic data types) show up in Rust, Swift, Kotlin, Scala, TypeScript and modern Java.

How this course works

Every haskell run example in this course is a complete program with a main :: IO (). When you press ▶ Run it is compiled by GHC 9.12 (the Glasgow Haskell Compiler, the standard implementation) on Compiler Explorer and executed, so you see real output, including real compiler errors. You can edit any example and run it again. The standard library and GHC's bundled packages (containers, text, mtl and friends) are available; third-party packages from Hackage are not.

Tip: Haskell feels different at first, especially if you know imperative languages. That is normal. Work through the lessons in order: each idea builds on the previous one, and by the Monad lessons the pieces will click together.

Test yourself

What does it mean that Haskell is *pure*?

  1. It has no type system
  2. Ordinary functions always return the same result for the same arguments and have no side effects
  3. It can only run in a browser
  4. Every value is computed immediately

Why can take 6 (map (^ 2) [1 ..]) finish even though [1 ..] is infinite?

  1. GHC limits lists to 1000 items
  2. Haskell is lazy and only computes the six elements that are needed
  3. take converts the list to an array
  4. It does not finish; it loops forever

What happens with putStrLn ("Total: " ++ 42)?

  1. It prints Total: 42
  2. It prints Total: and crashes
  3. The program is rejected at compile time with a type error
  4. The number is silently converted