Haskell features

Much of Haskell code is similar to standard mathematical notation in facility and syntax.

A simple Reverse Polish notation calculator expressed with the higher-order function foldl whose argument f is defined in a where clause using pattern matching and the type class Read: The empty list is the initial state, and f interprets one word at a time, either as a function name, taking two numbers from the head of the list and pushing the result back in, or parsing the word as a floating-point number and prepending it to the list.

This kind of a definition relies on lazy evaluation, an important feature of Haskell programming.

For an example of how the evaluation evolves, the following illustrates the values of fibs and tail fibs after the computation of six items and shows how zipWith (+) has produced four items and proceeds to produce the next item: The same function, written using Glasgow Haskell Compiler's parallel list comprehension syntax (GHC extensions must be enabled using a special command-line flag, here -XParallelListComp, or by starting the source file with {-# LANGUAGE ParallelListComp #-}): or with regular list comprehensions: or directly self-referencing: With stateful generating function: or with unfoldr: or scanl: Using data recursion with Haskell's predefined fixpoint combinator: The factorial we saw previously can be written as a sequence of functions: A remarkably concise function that returns the list of Hamming numbers in order: Like the various fibs solutions displayed above, this uses corecursion to produce a list of numbers on demand, starting from the base case of 1 and building new items based on the preceding part of the list.

Its companion function minus implements set difference: It is possible to generate only the unique multiples, for more efficient operation.

Slightly faster (but still very slow)[3] is this code by David Turner: Much faster is the optimal trial division algorithm or an unbounded sieve of Eratosthenes with postponed sieving in stages,[4] or the combined sieve implementation by Richard Bird,[5] or an even faster tree-like folding variant[6] with nearly optimal (for a list-based code) time complexity and very low space complexity achieved through telescoping multistage recursive production of primes: Working on arrays by segments between consecutive squares of primes, it's The shortest possible code is probably  nubBy (((>1) .)

For example, in a where clause: The two equations for the nested function prod are aligned vertically, which allows the semi-colon separator to be omitted.

The use of indentation to indicate program structure originates in Peter J. Landin's ISWIM language, where it was called the off-side rule.

One reason for wanting support for explicit delimiters is that it makes automatic generation of Haskell source code easier.

Binary operators can be partially applied using section notation: See List comprehension#Overview for the Haskell example.

An example of an ADT used to represent a person's name, sex and age might look like: The ST monad allows writing imperative programming algorithms in Haskell, using mutable variables (STRefs) and mutable arrays (STArrays and STUArrays).

Here is an example program (taken from the Haskell wiki page on the ST monad) that takes a list of numbers, and sums them, using a mutable variable: The STM monad is an implementation of Software Transactional Memory in Haskell.

The typical example is input/output (I/O), but monads are useful for many other purposes, including mutable state, concurrency and transactional memory, exception handling, and error propagation.

The following program reads a name from the command line and outputs a greeting message: The do-notation eases working with monads.

This do-expression is equivalent to, but (arguably) easier to write and understand than, the de-sugared version employing the monadic operators directly: The Haskell language definition includes neither concurrency nor parallelism, although GHC supports both.