HOME
        TheInfoList






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 each return a value, rather than a sequence of imperative statements which change the state of the program.

In functional programming, functions are treated as first-class citizens, meaning that they can be bound to names (including local identifiers), passed as arguments, and returned from other functions, just as any other data type can. This allows programs to be written in a declarative and composable style, where small functions are combined in a modular manner.

Functional programming is sometimes treated as synonymous with purely functional programming, a subset of functional programming which treats all functions as deterministic mathematical functions, or pure functions. When a pure function is called with some given arguments, it will always return the same result, and cannot be affected by any mutable state or other side effects. This is in contrast with impure procedures, common in imperative programming, which can have side effects (such as modifying the program's state or taking input from a user). Proponents of purely functional programming claim that by restricting side effects, programs can have fewer bugs, be easier to debug and test, and be more suited to formal verification.[1][2]

Functional programming has its roots in academia, evolving from the lambda calculus, a formal system of computation based only on functions. Functional programming has historically been less popular than imperative programming, but many functional languages are seeing use today in industry and education, including Common Lisp, Scheme,[3][4][5][6] Clojure, Wolfram Language,[7][8] Racket,[9] Erlang,[10][11][12] OCaml,[13][14] Haskell,[15][16] and F#.[17][18] Functional programming is also key to some languages that have found success in specific domains, like R in statistics,[19][20] J, K and Q in financial analysis, and XQuery/XSLT for XML.[21][22] Domain-specific declarative languages like SQL and Lex/Yacc use some elements of functional programming, such as not allowing mutable values.[23] In addition, many other programming languages support programming in a functional style or have implemented features from functional programming, such as C++11, Kotlin,[24] Perl,[25] PHP,[26] Python,[27] Raku,[28] and Scala.[29]

Simulating state

There are tasks (for example, maintaining a bank account balance) that often seem most naturally implemented with state. Pure functional programming performs these tasks, and I/O tasks such as accepting user input and printing to the screen, in a different way.

The pure functional programming language Haskell implements them using monads, derived from category theory. Monads offer a way to abstract certain types of computational patterns, including (but not limited to) modeling of computations with mutable state (and other side effects such as I/O) in an imperative manner without losing purity. While existing monads may be easy to apply in a program, given appropriate templates and examples, many students find them difficult to understand conceptually, e.g., when asked to define new monads (which is sometimes needed for certain types of libraries).[65]

Functional languages also simulate states by passing around immutable states. This can be done by making a function accept the state as one of its parameters, and return a new state together with the result, leaving the old state unchanged.[66]

Impure functional languages usually include a more direct method of managing mutable state. Clojure, for example, uses managed references that can be updated by applying pure functions to the current state. This kind of approach enables mutability while still promoting the use of pure functions as the preferred way to express computations.[citation needed]

Alternative methods such as Hoare logic and uniqueness have been developed to track side effects in programs. Some modern research languages use effect systems to make the presence of side effects explicit.[citation needed]

Functional programming languages are typically less efficient in their use of CPU and memory than imperative languages such as C and Pascal.[67] This is related to the fact that some mutable data structures like arrays have a very straightforward implementation using present hardware. Flat arrays may be accessed very efficiently with deeply pipelined CPUs, prefetched efficiently through caches (with no complex pointer chasing), or handled with SIMD instructions. It is also not easy to create their equally efficient general-purpose immutable counterparts. For purely functional languages, the worst-case slowdown is logarithmic in the number of memory cells used, because mutable memory can be represented by a purely functional data structure with logarithmic access time (such as a balanced tree).[68] However, such slowdowns are not universal. For programs that perform intensive numerical computations, functional languages such as OCaml and Clean are only slightly slower than C according to The Computer Language Benchmarks Game.[69] For programs that handle large matrices and multidimensional databases, array functional languages (such as J and K) were designed with speed optimizations.

Immutability of data can in many cases lead to execution efficiency by allowing the compiler to make assumptions that are unsafe in an imperative language, thus increasing opportunities for inline expansion.[

Immutability of data can in many cases lead to execution efficiency by allowing the compiler to make assumptions that are unsafe in an imperative language, thus increasing opportunities for inline expansion.[70]

Lazy evaluation may also speed up the program, even asymptotically, whereas it may slow it down at most by a constant factor (however, it may introduce memory leaks if used improperly). Launchbury 1993[53] discusses theoretical issues related to memory leaks from lazy evaluation, and O'Sullivan et al. 2008[71] give some practical advice for analyzing and fixing them. However, the most general implementations of lazy evaluation making extensive use of dereferenced code and data perform poorly on modern processors with deep pipelines and multi-level caches (where a cache miss may cost hundreds of cycles)[citation needed].

It is possible to use a functional style of programming in languages that are not traditionally considered functional languages.[72] For example, both D[73] and Fortran 95[46] explicitly support pure functions.

JavaScript, Lua[74] and Python had first class functions from the

JavaScript, Lua[74] and Python had first class functions from their inception.[75] Python had support for "lambda", "map", "reduce", and "filter" in 1994, as well as closures in Python 2.2,[76] though Python 3 relegated "reduce" to the functools standard library module.[77] First-class functions have been introduced into other mainstream languages such as PHP 5.3, Visual Basic 9, C# 3.0, C++11, and Kotlin.[24][citation needed]

In PHP, anonymous classes, closures and lambdas are fully supported. Libraries and language extensions for immutable data structures are being developed to aid programming in the functional style.

In Java, anonymous classes can sometimes be used to simulate closures;[78] however, anonymous classes are not always proper replacements to closures because they have more limited capabilities.[79] Java 8 supports lambda expressions as a replacement for some anonymous classes.[80]

In C#, anonymous classes are not necessary, because closures and lambdas are fully supported. Libraries and language extensions for immutable data structures are being developed to aid programming in the functional style in C#.

Many object-oriented design patterns are expressible in functional programming terms: for example, the strategy pattern simply dictates use of a higher-order function, and the visitor pattern roughly corresponds to a catamorphism, or fold.

Similarly, the idea of immutable data from functional programming is often included in imperative programming languages,[81] for example the tuple in Python, which is an immutable array.

Functional programming is an active area of research in the field of programming language theory. There are several peer-reviewed publication venues focusing on functional programming, including the International Conference on Functional Programming, the Journal of Functional Programming, and the Symposium on Trends in Functional Programming.

Industry

Functional programming has seen use in a wide variety of industrial applications. For example, Erlang, which was developed by the Swedish company Eric

Functional programming has seen use in a wide variety of industrial applications. For example, Erlang, which was developed by the Swedish company Ericsson in the late 1980s, was originally used to implement fault-tolerant telecommunications systems,[11] but has since become popular for building a range of applications at companies such as Nortel, Facebook, Électricité de France and WhatsApp.[10][12][82][83][84] Scheme, a dialect of Lisp, was used as the basis for several applications on early Apple Macintosh computers,[3][4] and has been applied to problems such as training simulation software[5] and telescope control.[6] OCaml, which was introduced in the mid-1990s, has seen commercial use in areas such as financial analysis,[13] driver verification, industrial robot programming, and static analysis of embedded software.[14] Haskell, though initially intended as a research language,[16] has also been applied by a range of companies, in areas such as aerospace systems, hardware design, and web programming.[15][16]

Other functional programming languages that have seen use in industry include Scala,[85] Scala,[85] F#,[17][18] Wolfram Language,[7] Lisp,[86] Standard ML,[87][88] and Clojure.[89]

Functional "platforms" have been popular in finance for risk analytics (particularly with the larger investment banks). Risk factors are coded as functions that form interdependent graphs (categories) to measure correlations in market shifts not unlike Gröbner basis optimizations but also for regulatory compliance such as Comprehensive Capital Analysis and Review. Given the use of OCAML or CAML variations in finance, these systems are sometimes considered related to a categorical abstract machine or CAM. Indeed, functional programming is heavily influenced by category theory.

Many universities teach or have taught functional programming as part of their undergraduate Computer Science degrees.[90][91][92][93] Some use it as their introduction to programming,[93] while others teach it after teaching imperative programming.[92][94]

Outside of computer science, functional programming is being used as a method to teach problem solving, algebra and geometric concepts.[95] It has also been used as a tool to teach classical mechanics

Outside of computer science, functional programming is being used as a method to teach problem solving, algebra and geometric concepts.[95] It has also been used as a tool to teach classical mechanics in Structure and Interpretation of Classical Mechanics.