Nepedia
Back to Discover
Advanced Reader
💻

Functional programming

technology Maturity 7-9 Next article →

Functional programming is a specific paradigm in computer science. In this approach, programmers construct software by applying and composing functions. It is a declarative paradigm, which means it focuses on what to solve rather than how to change state. Instead of using a sequence of imperative statements to update a program's running state, functional programming uses trees of expressions. These expressions map specific input values to new output values. This style allows for a modular approach where small, manageable functions are combined to create complex systems.

A core mechanic of this paradigm is treating functions as first-class entities. This term means functions have no restrictions on their use within a program. They can be bound to names or local identifiers just like any other data type. Programmers can also pass functions as arguments to other functions. Furthermore, a function can return another function as its result. This capability leads to the use of higher-order functions. A higher-order function is a mathematical concept where a function operates on other functions. This allows for techniques like currying, which is the process of applying a function to its arguments one at a time. Each step returns a new function that accepts the next argument.

Within this field, the concept of pure functions is vital. A pure function is a deterministic mathematical function. This means that when a pure function is called with specific arguments, it will always return the same result. Pure functions are defined by their lack of side effects. A side effect is an unintended change, such as modifying the program's state or taking user input. Because pure functions do not rely on or change mutable state, they offer several technical advantages. If the result of a pure expression is not needed, a compiler can safely remove it. If there is no data dependency between two pure expressions, they can be performed in parallel. This makes the code thread-safe and easier to optimize through methods like memoization, which is a form of caching.

The history of functional programming is rooted deeply in academic mathematics. In the 1930s, Alonzo Church developed the lambda calculus. This is a formal system of computation built entirely from function application. In 1937, Alan Turing proved that lambda calculus and Turing machines are equivalent models of computation. This proved that lambda calculus is Turing complete. Other foundational work includes combinatory logic, developed by Moses Schönfinkel and Haskell Curry in the 1920s and 1930s. Church also created the simply typed lambda calculus. This system assigned data types to all terms and became the basis for statically typed functional programming.

Practical implementation began in the late 1950s with the creation of Lisp. John McCarthy developed Lisp at the Massachusetts Institute of Technology for the IBM 700/7000 series of scientific computers. Lisp functions used Church's lambda notation and included a label construct for recursive functions. Other early milestones include the Information Processing Language in 1956 and the SECD machine in the mid-1960s. In 1977, John Backus delivered a Turing Award lecture regarding a functional style. He proposed that programs could be built hierarchically through an algebra of programs. This emphasized the principle of compositionality, where complex structures are built from simpler ones.

Today, functional programming is used across many different industries and domains. There are many specialized languages, such as Haskell, which began as an open standard in 1987. Other languages like Erlang and Elixir are used in modern industry. Some languages are designed for specific tasks, such as R for statistics or JavaScript for the Web. Financial analysis often relies on languages like J, K, and Q. Even domain-specific languages like SQL and XQuery use functional elements, such as the restriction of mutable values. Furthermore, many mainstream languages have adopted functional features. These include C++ since C++11, Java since Java 8, Python, Rust, and Go.

Functional programming connects deeply to other fields like formal verification and artificial intelligence. The language Lean is used specifically to verify mathematical theorems. In the field of artificial intelligence, the Logic Theorist program used a paradigm that embeds functional programming to prove theorems from Principia Mathematica. The connection between functional programs and constructive proofs was also advanced by Per Martin-Löf through intuitionistic type theory. This work has influenced modern approaches to interactive theorem proving. By restricting side effects, proponents argue that programs become easier to debug, test, and verify formally.

738 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.