Haskell

Haskell was defined in 1990 by an academic committee to be a common, open, purely functional language, and is named after logician Haskell Curry. It combines purity, laziness and a strong static type system. It is where many ideas later adopted by mainstream languages were tested first.

Paradigm: Functional Programming

What Makes Haskell Special

  • Purity. A function's result depends only on its arguments. Effects such as printing or reading a file appear in the type, for example IO (), so they cannot hide.
  • Lazy evaluation. A value is computed only when needed. This allows infinite lists such as [2 ..] and lets you separate producing data from consuming it.
  • Type inference and type classes. The compiler infers most types, and type classes (Eq, Ord, Functor, Monad) provide overloading with clear laws.
  • Algebraic data types and pattern matching. You define data as a set of cases and write functions that match each case, and the compiler warns about missing ones.
  • Monads for effects. Sequencing effects is done with a common interface, so do notation reads like imperative code while staying pure.

Example: An infinite list of primes

primes :: [Int]
primes = sieve [2 ..]
  where
    sieve (p : xs) = p : sieve [x | x <- xs, x `mod` p /= 0]

main :: IO ()
main = do
  print (take 10 primes)
  print (sum (takeWhile (< 100) (map (^ 2) [1 ..])))

How It Works

  • primes :: [Int] is a type signature: primes is a list of integers. It is optional, but good style.
  • [2 ..] is the infinite list of integers from 2, and laziness makes this legal: only the elements requested are ever computed.
  • sieve keeps the first element and removes its multiples from the rest, recursively. It is the classic (though not the fastest) sieve of Eratosthenes.
  • The first line prints [2,3,5,7,11,13,17,19,23,29]. The second squares an infinite list, stops before 100 and sums the squares to 285.
  • main :: IO () shows the effect in the type: this action does input or output.

History and Where It Is Used

Haskell is used in finance (Standard Chartered), large-scale spam and abuse detection at Meta, compilers and static analysis, blockchain and formal tools, and for correctness-critical services. Its compiler GHC is a research platform, and its ideas appear in Rust, Swift, Kotlin, Scala and C# (LINQ, async).

Learn More