In
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to practical disciplines (includin ...
, purely functional programming usually designates a
programming paradigm
Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms.
Some paradigms are concerned mainly with implications for the execution model of the language, s ...
—a style of building the structure and elements of computer programs—that treats all
computation
Computation is any type of arithmetic or non-arithmetic calculation that follows a well-defined model (e.g., an algorithm).
Mechanical or electronic devices (or, historically, people) that perform computations are known as '' computers''. An esp ...
as the evaluation of
mathematical functions.
Program state and
mutable objects are usually modeled with
temporal logic In logic, temporal logic is any system of rules and symbolism for representing, and reasoning about, propositions qualified in terms of time (for example, "I am ''always'' hungry", "I will ''eventually'' be hungry", or "I will be hungry ''until'' I ...
, as explicit variables that represent the program state at each step of a program execution: a variable state is passed as an
input parameter of a state-transforming function, which returns the updated state as part of its return value. This style handles state changes without losing the
referential transparency
In computer science, referential transparency and referential opacity are properties of parts of computer programs. An expression is called ''referentially transparent'' if it can be replaced with its corresponding value (and vice-versa) witho ...
of the program expressions.
Purely functional programming consists of ensuring that functions, inside the
functional paradigm, will only depend on their arguments, regardless of any global or local state. A pure functional subroutine only has visibility of changes of state represented by state variables included in its scope.
Difference between pure and impure functional programming
The exact difference between pure and impure functional programming is a matter of controversy. Sabry's proposed definition of purity is that all common
evaluation strategies (call-by-name, call-by-value, and call-by-need) produce the same result, ignoring strategies that error or diverge.
A program is usually said to be functional when it uses some concepts of
functional programming
In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that ...
, such as
first-class functions and
higher-order functions. However, a first-class function need not be purely functional, as it may use techniques from the
imperative paradigm, such as
arrays or input/output
methods that use mutable cells, which update their state as side effects. In fact, the earliest programming languages cited as being functional,
IPL and
Lisp
A lisp is a speech impairment in which a person misarticulates sibilants (, , , , , , , ). These misarticulations often result in unclear speech.
Types
* A frontal lisp occurs when the tongue is placed anterior to the target. Interdental lispi ...
, are both "impure" functional languages by Sabry's definition.
Purely functional data structure
In computer science, a purely functional data structure is a data structure that can be implemented in a purely functional language. The main difference between an arbitrary data structure and a purely functional one is that the latter is (stron ...
s are
persistent. Persistency is required for functional programming; without it, the same computation could return different results. Functional programming may use persistent non-purely functional
data structure
In computer science, a data structure is a data organization, management, and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the rel ...
s, while those data structures may not be used in purely functional programs.
Properties of purely functional programming
Strict versus non-strict evaluation
Each
evaluation strategy
In a programming language, an evaluation strategy is a set of rules for evaluating expressions. The term is often used to refer to the more specific notion of a ''parameter-passing strategy'' that defines the kind of value that is passed to the f ...
which ends on a purely functional program returns the same result. In particular, it ensures that the programmer does not have to consider in which order programs are evaluated, since
eager evaluation will return the same result as
lazy evaluation
In programming language theory, lazy evaluation, or call-by-need, is an evaluation strategy which delays the evaluation of an expression until its value is needed ( non-strict evaluation) and which also avoids repeated evaluations ( sharing).
T ...
. However, it is still possible that an eager evaluation may not terminate while the lazy evaluation of the same program halts.
An advantage of this is that lazy evaluation can be implemented much more easily; as all expressions will return the same result at any moment (regardless of program state), their evaluation can be delayed as much as necessary.
Parallel computing
Purely functional programming simplifies
parallel computing since two purely functional parts of the evaluation never interact.
Data structures
Purely functional
data structure
In computer science, a data structure is a data organization, management, and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the rel ...
s are often represented in a different way than their
imperative counterparts.
''Purely functional data structures''
by Chris Okasaki, Cambridge University Press
Cambridge University Press is the university press of the University of Cambridge. Granted letters patent by Henry VIII of England, King Henry VIII in 1534, it is the oldest university press in the world. It is also the King's Printer.
Cambr ...
, 1998, For example, array
An array is a systematic arrangement of similar objects, usually in rows and columns.
Things called an array include:
{{TOC right
Music
* In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
with constant-time access and update is a basic component of most imperative languages and many imperative data-structures, such as hash table
In computing, a hash table, also known as hash map, is a data structure that implements an associative array or dictionary. It is an abstract data type that maps keys to values. A hash table uses a hash function to compute an ''index'', ...
and binary heap, are based on arrays. Arrays can be replaced by map or random access list, which admits purely functional implementation, but the access and update time is logarithm
In mathematics, the logarithm is the inverse function to exponentiation. That means the logarithm of a number to the base is the exponent to which must be raised, to produce . For example, since , the ''logarithm base'' 10 of ...
ic. Therefore, purely functional data structures can be used in languages which are non-functional, but they may not be the most efficient tool available, especially if persistency is not required.
In general, conversion of an imperative program to a purely functional one also requires ensuring that the formerly-mutable structures are now explicitly returned from functions that update them, a program structure called store-passing style.
Purely functional language
A purely functional language is a language which only admits purely functional programming. Purely functional programs can however be written in languages which are not purely functional.
References
{{DEFAULTSORT:Functional programming
Programming paradigms