Safekipedia

Functional programming

Discoverer · ages 10+Adapted from Wikipedia

For a different style of programming, see Procedural programming. In computer science, functional programming is a programming paradigm where programs are built by applying and composing functions. It is a declarative programming paradigm. Its function definitions are trees of expressions that map values to other values, rather than a list of commands called statements that change the running state of the program.

In functional programming, functions are treated as first-class entities. This means they can be given names (including local identifiers), passed as arguments, and returned from other functions, just like any other data type. This lets programmers write programs in a declarative and composable way, where small functions are joined together in a modular manner.

Functional programming is sometimes treated as the same thing as purely functional programming, which is a smaller part of it. Purely functional programming treats all functions as deterministic mathematical functions, also called pure functions. When a pure function is called with the same arguments, it always gives the same result, and it cannot be changed by any mutable state or other side effects. Other procedures, which are common in imperative programming, can have side effects, such as changing the program's state or taking input from a user. Supporters of purely functional programming say that limiting side effects can mean fewer bugs, programs that are easier to debug and test, and programs that are better suited to formal verification.

Functional programming has its roots in academia and grew out of the lambda calculus, a formal system of computation based only on functions. It has historically been less popular than imperative programming, but many functional languages are used today in industry and education. These include Common Lisp, Scheme, Clojure, Wolfram Language, Racket, Erlang, Elixir, OCaml, Haskell, and F#. Lean is a functional programming language that is commonly used for verifying mathematical theorems.

Functional programming is also important in some languages that have succeeded in specific areas, such as JavaScript on the Web, R in statistics, J, K, and Q in financial analysis, and XQuery/XSLT for XML. Some domain-specific declarative languages, like SQL and Lex/Yacc, use some elements of functional programming, such as not allowing mutable values. Many other programming languages support programming in a functional style or have added features from functional programming. Examples include D (since 2007 with the release of D2), C++ (since C++11), C#, Kotlin, Perl, PHP, Python, Go, Rust, Raku, Scala, and Java (since Java 8).

History

The lambda calculus, developed in the 1930s by Alonzo Church, is a formal system of computation built from function application. In 1937 Alan Turing proved that it and Turing machines are equivalent models of computation, which showed that it is Turing complete. Lambda calculus forms the basis of all functional programming languages. An equivalent theory, combinatory logic, was developed by Moses Schönfinkel and Haskell Curry in the 1920s and 1930s. Church later developed a weaker system, the simply typed lambda calculus, which gave a data type to all terms, and this forms the basis for statically typed functional programming. The first high-level functional language, Lisp, was developed in the late 1950s for the IBM 700/7000 series of scientific computers by John McCarthy at Massachusetts Institute of Technology (MIT). Lisp used Church's lambda notation, extended to allow recursive functions, and it first introduced many features of functional programming. Early Lisps were multi-paradigm languages, and later dialects such as Scheme, Clojure, Dylan, and Julia tried to build a cleaner functional core, while Common Lisp was designed to keep the features of the older dialects it replaced.

The Information Processing Language (IPL), from 1956, is sometimes called the first computer-based functional programming language, though it is an assembly-style language for handling lists of symbols and depends heavily on changing those lists, which is a non-functional style. Kenneth E. Iverson developed APL in the early 1960s, described in his 1962 book A Programming Language (ISBN 9780471430148), and APL was the main influence on John Backus's FP. In the early 1990s, Iverson and Roger Hui created J, and in the mid-1990s Arthur Whitney created K, which is used commercially in financial industries along with its descendant Q. In the mid-1960s, Peter Landin invented the SECD machine, the first abstract machine for a functional language, and proposed the ISWIM language. In his 1977 Turing Award lecture, Backus presented FP and described functional programs as built from "combining forms" that follow the principle of compositionality; his paper encouraged research into functional programming, though it focused on function-level programming. The language ML was created in 1973 by Robin Milner at the University of Edinburgh, and David Turner developed SASL at the University of St Andrews. ML later grew into dialects, the most common now being OCaml and Standard ML. In the 1970s, Guy L. Steele and Gerald Jay Sussman developed Scheme, the first Lisp dialect to use lexical scoping and to require tail-call optimization. In the 1980s, Per Martin-Löf developed intuitionistic type theory, which led to new approaches to interactive theorem proving. The lazy functional language Miranda, developed by David Turner, appeared in 1985 and strongly influenced Haskell, which began in 1987 as an open standard for functional programming research. More recently, functional programming has found use in niches such as the OpenSCAD language, built on the CGAL framework, and it continues to be used in commercial settings.

Concepts

Main articles: First-class function and Higher-order function

Main article: Pure function

Main article: Recursion (computer science)

Main article: Evaluation strategy

Main article: Type system

Main article: Referential transparency

Main article: Purely functional data structure

Some ideas are special to functional programming and are mostly unlike imperative programming and object-oriented programming, though many languages mix several styles. Higher-order functions take other functions as inputs or give them back as results, like the differential operator d/dx, which returns the derivative of a function. Currying applies a function to one argument at a time, so the successor function can be written as addition applied to the natural number one. Pure functions have no side effects, so they give the same result for the same inputs, which helps programs run faster. Functional programs often repeat actions through recursive functions instead of loops, and a special form called tail recursion can be turned into the same code a loop would use.

Functional languages also differ in how they handle function inputs. With strict evaluation, all inputs are worked out before a function runs. With lazy evaluation, inputs are only worked out when they are needed. Many functional languages use type systems to check programs before they run, and in functional programs a variable's value never changes once it is set, which keeps the programs predictable. Because of this, data structures are often built in different ways, such as using maps in place of arrays, and they can keep previous versions of themselves unchanged.

Comparison to imperative programming

Functional programming works very differently from imperative programming. The biggest differences come from the fact that functional programming avoids side effects, which imperative programming uses to keep track of changing information and to handle input and output. Instead of using a loop to change a list, a functional program often uses a "map" function, which takes a function and a list and returns a new list. Functional code can also help avoid mistakes such as off-by-one errors, which can be a problem in large, complicated imperative code (see Greenspun's tenth rule).

Some tasks, such as keeping track of a bank account balance, seem most natural with changing state, and pure functional languages handle them in a different way. The language Haskell uses monads, which come from category theory, though many students find them hard to understand. Other functional languages pass unchanging state into functions and get back a new state. Languages such as Clojure allow changing state through managed references. Researchers have also developed ways to track side effects, such as Hoare logic, uniqueness, and effect systems. Functional languages usually need more processing power and memory than imperative languages such as C and Pascal, although languages like OCaml and Clean are only slightly slower than C for number-heavy tasks. Functional ideas can also help with doing many things at once, using message passing as in the actor model found in Erlang/Elixir and Akka. Lazy evaluation can speed up a program, but it may cause memory leaks if used improperly. Rust is known for its approach to unchangeable references.

Comparison to logic programming

Logic programming can be seen as a wider version of functional programming, where functions are a special kind of relation. A function has a strict pattern of inputs and outputs, but a relation can be asked questions in any direction. For example, the mother relation can be used to find a child's mother, or to find the children of a mother.

Functional syntax is a shorter way to write nested functions than relational syntax. However, the two are mostly different ways of writing the same idea. In Ciao Prolog, relations can also be nested like functions, and Ciao changes this function-like notation into relational form before running the program.

Applications

Functional programming is used in many ways. Spreadsheets can be seen as a simple kind of functional programming system, with a zeroth-order style. However, spreadsheets usually cannot use higher-order functions or reuse code, and some cannot use recursion. Some extensions have been made for spreadsheet programs, but they remain mostly academic. Because of their composability, functional programming ideas can suit microservices-based systems. Functional programming is an active area of research in programming language theory, and researchers publish their work in peer-reviewed venues such as the International Conference on Functional Programming, the Journal of Functional Programming, and the Symposium on Trends in Functional Programming. Many universities teach functional programming, and some teach it before other methods. It is also used outside computer science to teach problem-solving, algebra, geometry, and classical mechanics, as in the book Structure and Interpretation of Classical Mechanics. Scheme has been a popular choice for teaching programming for years.

Functional programming has been used in many industries. Erlang, developed by the Swedish company Ericsson in the late 1980s, was first used to build fault-tolerant telecommunications systems. It has since become popular at companies such as Nortel, Facebook, Électricité de France and WhatsApp. Scheme, a dialect of Lisp, was used for several programs on early Apple Macintosh computers and for tasks such as simulation software and telescope control. OCaml, introduced in the mid-1990s, has been used in financial analysis, driver verification, industrial robot programming, and static analysis of embedded software. Haskell, first intended as a research language, has also been used in aerospace systems, hardware design, and web programming. Other functional languages used in industry include Scala, F#, Wolfram Language, Lisp, Standard ML and Clojure. Scala has been widely used in Data science, while ClojureScript, Elm and PureScript are used for frontend programming. Elixir's Phoenix framework is used by some popular commercial projects, such as Font Awesome and Allegro's classified ads platform Allegro Lokalnie. In finance, functional "platforms" have been popular for risk analysis, especially at large investment banks, where risk factors are coded as functions. These systems are sometimes considered related to a categorical abstract machine, and functional programming is heavily influenced by category theory. Some of these systems also resemble Gröbner basis optimizations and follow regulatory frameworks such as Comprehensive Capital Analysis and Review.

Quiz time!

5 questions about Functional programming

Question 1 of 5

In functional programming, what can a function be treated like?

Keep exploring

This article is a child-friendly adaptation of the Wikipedia article on Functional programming, available under CC BY-SA 4.0.