In January 1999, the Haskell 98 language standard was originally published as "The Haskell 98 Report". In January 2003, a revised version was published as "Haskell 98 Language and Libraries: The Revised Report". The language continues to evolve rapidly, with the Hugs and GHC implementation (see below) representing the current de facto standard. In early 2006, the process of defining a successor to the Haskell 98 standard, informally named Haskell′ ("Haskell Prime"), was begun. This process is intended to produce a minor revision of Haskell 98.
Several variants have been developed: parallelizable versions from MIT and Glasgow University, both called Parallel Haskell; more parallel and distributed versions called Distributed Haskell (formerly Goffin) and Eden; a speculatively evaluating version called Eager Haskell and several object oriented versions: Haskell++, O'Haskell and Mondrian.
Concurrent Clean is a close relative of Haskell, whose biggest deviation from Haskell is in the use of uniqueness types for input instead of monads.
factorial :: Integer -> Integer
factorial 0 = 1
factorial n | n > 0 = n * factorial (n-1)
Or in one line:
factorial n = if n > 0 then n * factorial (n-1) else 1
This describes the factorial as a recursive function, with one terminating base case. It is similar to the descriptions of factorials found in mathematics textbooks. Much of Haskell code is similar to standard mathematical notation in facility and syntax.
The first line of the factorial function describes the types of this function; while it is optional, it is considered to be good style to include it. It can be read as the function factorial (factorial) has type (::) from integer to integer (Integer -> Integer). That is, it takes an integer as an argument, and returns another integer. The type of a definition is inferred automatically if the programmer didn't supply a type annotation.
The second line relies on pattern matching, an important feature of Haskell. Note that parameters of a function are not in parentheses but separated by spaces. When the function's argument is 0 (zero) it will return the integer 1 (one). For all other cases the third line is tried. This is the recursion, and executes the function again until the base case is reached.
A guard protects the third line from negative numbers for which a factorial is undefined. Without the guard this function would recurse through all negative numbers without ever reaching the base case of 0. As it is, the pattern matching is not complete: if a negative integer is passed to the fac function as an argument, the program will fail with a runtime error. A final case could check for this error condition and print an appropriate error message instead.
The "Prelude" is a number of small functions analogous to C's standard library. Using the Prelude and writing in the point-free style of unspecified arguments, it becomes:
fac = product . enumFromTo 1
The above is close to mathematical definitions such as (see function composition) with the dot acting as the function composition operator, and indeed, it is not an assignment of a numeric value to a variable.
In the Hugs interpreter, you often need to define the function and use it on the same line separated by a where or let..in, meaning you need to enter this to test the above examples and see the output 120: let { fac 0 = 1; fac n | n > 0 = n * fac (n-1) } in fac 5 or
fac 5 where fac = product . enumFromTo 1
The GHCi interpreter doesn't have this restriction and function definitions can be entered on one line and referenced later.
foldl whose argument f is defined in a where clause using pattern matching and the type class Read:
calc :: String -> [Float]
calc = foldl f [] . words
where
f (x:y:zs) "+" = y+x:zs
f (x:y:zs) "-" = y-x:zs
f (x:y:zs) "*" = y*x:zs
f (x:y:zs) "/" = y/x:zs
f xs y = read y : xs
The empty list is the initial state, and f interprets one word at a time, either matching 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.
The following definition produces the list of Fibonacci numbers in linear time:
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
The infinite list is produced by corecursion — the latter values of the list are computed on demand starting from the initial two items 0 and 1. 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:
fibs = 0 : 1 : 1 : 2 : 3 : 5 : ...
+ + + + + +
tail fibs = 1 : 1 : 2 : 3 : 5 : ...
= = = = = =
zipWith ... = 1 : 2 : 3 : 5 : 8 : ...
fibs = 0 : 1 : 1 : 2 : 3 : 5 : 8 : ...The same function, written using GHC's parallel list comprehension syntax (GHC extensions must be enabled using a special command-line flag '-fglasgow-exts'; see GHC's manual for more):
fibs = 0 : 1 : [a+b | a <- fibs | b <- tail fibs ]
The factorial we saw previously can be written as a sequence of functions:
fac n = (foldl (.) id [x -> x*k | k <- [1..n]]) 1
A remarkably concise function that returns the list of Hamming numbers in order:
hamming = 1 : map (2*) hamming `merge` map (3*) hamming `merge` map (5*) hamming
where merge (x:xs) (y:ys)
| x < y = x : xs `merge` (y:ys)
| x > y = y : (x:xs) `merge` ys
| otherwise = x : xs `merge` ys
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.
In this case the producer merge is defined in a where clause and used as an operator by enclosing it in back-quotes.
The branches of the guards define how merge merges two ascending lists into one ascending list without duplicate items.
Jan-Willem Maessen, in 2002, and Simon Peyton Jones, in 2003, discussed problems associated with lazy evaluation while also acknowledging the theoretical motivation for it, in addition to purely practical considerations such as improved performance. They note that, in addition to adding some performance overhead, laziness makes it more difficult for programmers to reason about the performance of their code (specifically with regard to space usage).
Bastiaan Heeren, Daan Leijen, and Arjan van IJzendoorn in 2003 also observed some stumbling blocks for Haskell learners, "The subtle syntax and sophisticated type system of Haskell are a double edged sword—highly appreciated by experienced programmers but also a source of frustration among beginners, since the generality of Haskell often leads to cryptic error messages." To address these, they developed an advanced interpreter called Helium which improved the user-friendliness of error messages by limiting the generality of some Haskell features, and in particular removing support for type classes.