ⓘ Haskell features. A simple example that is often used to demonstrate the syntax of functional languages is the factorial function for non-negative integers, sho ..


ⓘ Haskell features

A simple example that is often used to demonstrate the syntax of functional languages is the factorial function for non-negative integers, shown in Haskell:

Or in one line:

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 type 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 has type from integer to 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 didnt 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 functions 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.

Using the product function from the Prelude, a number of small functions analogous to Cs standard library, and using the Haskell syntax for arithmetic sequences, the factorial function can be expressed in Haskell as follows:

Here. It is quite slow.


1.1. Syntax Function calls

Applying a function f to a value x is expressed as simply f x.

Haskell distinguishes function calls from infix operators syntactically, but not semantically. Function names which are composed of punctuation characters can be used as operators, as can other function names if surrounded with backticks; and operators can be used in prefix notation if surrounded with parentheses.

This example shows the ways that functions can be called:

Functions which are defined as taking several parameters can always be partially applied. Binary operators can be partially applied using section notation:


1.2. Syntax List comprehensions

See List comprehension#Overview for the Haskell example.


1.3. Syntax Pattern matching

Pattern matching is used to match on the different constructors of algebraic data types. Here are some functions, each using pattern matching on each of the types below:

Using the above functions, along with the map function, we can apply them to each element of a list, to see their results:

  • Abstract Types
  • Lists

1.4. Syntax Tuples

Tuples in haskell can be used to hold a fixed number of elements. They are used to group pieces of data of differing types:

Tuples are commonly used in the zip* functions to place adjacent elements in separate lists together in tuples zip4 to zip7 are provided in the Data.List module:

In the GHC compiler, tuples are defined with sizes from 2 elements up to 62 elements.

  • Records

2. Namespaces

In the #More_complex_examples section above, calc is used in two senses, showing that there is a Haskell type class namespace and also a namespace for values:

  • a Haskell type class for calc. The domain and range can be explicitly denoted in a Haskell type class.
  • a Haskell value, formula, or expression for calc.

3. Typeclasses and polymorphism

Algebraic data types

Algebraic data types are used extensively in Haskell. Some examples of these are the built in list, Maybe and Either types:

Users of the language can also define their own abstract data types. An example of an ADT used to represent a persons name, sex and age might look like:

Type system

  • Functional Dependencies
  • Type classes
  • Multi-Parameter Type Classes
  • Type defaulting
  • Overloaded Literals
  • Higher Kinded Polymorphism


4. Monads and input/output

  • Applications
  • Monadic IO
  • Do-notation
  • Exceptions
  • References
  • Overview of the monad framework

STM monad

The STM monad is an implementation of Software Transactional Memory in Haskell. It is implemented in the GHC compiler, and allows for mutable variables to be modified in transactions.


4.1. Monads and input/output ST monad

The ST monad allows programmers to write imperative algorithms in Haskell, using mutable variables STRefs and mutable arrays STArrays and STUArrays. The advantage of the ST monad is that it allows programmers to write code that has internal side effects, such as destructively updating mutable variables and arrays, while containing these effects inside the monad. The result of this is that functions written using the ST monad appear completely pure to the rest of the program. This allows programmers to produce imperative code where it may be impractical to write functional code, while still keeping all the safety that pure code provides.

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:


4.2. Monads and input/output STM monad

The STM monad is an implementation of Software Transactional Memory in Haskell. It is implemented in the GHC compiler, and allows for mutable variables to be modified in transactions.


4.3. Monads and input/output Arrows

  • Applicative Functors
  • Arrows

As Haskell is a pure functional language, functions cannot have side effects. Being non-strict, it also does not have a well-defined evaluation order. This is a challenge for real programs, which among other things need to interact with an environment. Haskell solves this with monadic types that leverage the type system to ensure the proper sequencing of imperative constructs. The typical example is I/O, but monads are useful for many other purposes, including mutable state, concurrency and transactional memory, exception handling, and error propagation.

Haskell provides a special syntax for monadic expressions, so that side-effecting programs can be written in a style similar to current imperative programming languages; no knowledge of the mathematics behind monadic I/O is required for this. 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:

See also wikibooks:Transwiki:List of hello world programs#Haskell for another example that prints text.


5. Concurrency

The Haskell language definition itself does not include either concurrency or parallelism, although GHC supports both.

Concurrent Haskell is an extension to Haskell that provides support for threads and synchronization. GHCs implementation of Concurrent Haskell is based on multiplexing lightweight Haskell threads onto a few heavyweight OS threads, so that Concurrent Haskell programs run in parallel on a multiprocessor. The runtime can support millions of simultaneous threads.

The GHC implementation employs a dynamic pool of OS threads, allowing a Haskell thread to make a blocking system call without blocking other running Haskell threads. Hence the lightweight Haskell threads have the characteristics of heavyweight OS threads, and the programmer is unaware of the implementation details.

Recently, Concurrent Haskell has been extended with support for Software Transactional Memory STM, which is a concurrency abstraction in which compound operations on shared data are performed atomically, as transactions. GHCs STM implementation is the only STM implementation to date to provide a static compile-time guarantee preventing non-transactional operations from being performed within a transaction. The Haskell STM library also provides two operations not found in other STMs: retry and orElse, which together allow blocking operations to be defined in a modular and composable fashion.

Free and no ads
no need to download or install

Pino - logical board game which is based on tactics and strategy. In general this is a remix of chess, checkers and corners. The game develops imagination, concentration, teaches how to solve tasks, plan their own actions and of course to think logically. It does not matter how much pieces you have, the main thing is how they are placement!

online intellectual game →