Haskell Introduction

Haskell is a purely functional, lazily evaluated, statically typed programming language. Named after logician Haskell Curry, it is the gold standard for functional programming and has influenced languages like Rust, Elm, TypeScript, and Scala.

What makes Haskell different?

Most languages are imperative — you write a sequence of instructions that change state. Haskell is functional — you compose pure functions that transform data without side effects.

HASKELL
-- Imperative (Java/Python style): "do this, then this, then that"
result = []
for i in range(10):
    if i % 2 == 0:
        result.append(i * i)

-- Functional (Haskell style): "what I want"
result = map (^2) (filter even [0..9])

Key concepts

Concept Haskell Traditional
Purity Functions have no side effects Functions can modify anything
Immutability Variables never change Variables can be reassigned
Lazy evaluation Compute only what's needed Compute everything upfront
Strong types Types catch errors at compile time Many errors found at runtime
Type classes Ad-hoc polymorphism (like interfaces) Interface/traits

Where Haskell is used

  • Finance — Standard Chartered, Barclays, Jane Street use Haskell for trading systems
  • Tech — Facebook's spam filter, GitHub's semantic versioning library
  • Compilers — GHC (Haskell's own compiler) is written in Haskell
  • Data engineering — Pandoc (universal document converter) is Haskell

Hello World

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

Save as Main.hs and compile:

Bash
ghc Main.hs -o hello
./hello

Or run directly:

Bash
runhaskell Main.hs

Basic Types and Functions

HASKELL
-- Integers
x :: Int
x = 42

-- Floats
pi :: Double
pi = 3.14159

-- Strings (actually [Char])
greeting :: String
greeting = "Hello, Nepal!"

-- Booleans
isActive :: Bool
isActive = True

-- Functions (type signature goes on the line before)
add :: Int -> Int -> Int
add a b = a + b

-- Functions are curried by default!
-- add 3 4 is actually (add 3) 4
add5 :: Int -> Int
add5 = add 5

Guards

HASKELL
grade :: Int -> String
grade score
    | score >= 90 = "A"
    | score >= 80 = "B"
    | score >= 70 = "C"
    | score >= 60 = "D"
    | otherwise   = "F"

What you will learn

Chapter Topic
Basics Functions, pattern matching, lists
Types Algebraic data types, type classes
FP Higher-order functions, composition
Monads IO, Maybe, chaining effects
IO Reading files, user input
Real World Projects, testing, tools