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 map values to other values, rather than a sequence of imperative statements which update the running state of the program. In functional programming, functions are treated as first-class entities, 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 that treats all functions as deterministic mathematical functions, or pure functions. When a pure function is called with some given...
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 map values to other values, rather than a sequence of imperative statements which update the running state of the program. In functional programming, functions are treated as first-class entities, 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 that 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. 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, Clojure, Wolfram Language, Racket, Erlang, Elixir, OCaml, Haskell, and F#. Lean is a functional programming language commonly used for verifying mathematical theorems. Functional programming is also key to some languages that have found success in specific domains, like JavaScript in the Web, R in statistics, J, K and Q in financial analysis, and XQuery/XSLT for XML. Domain-specific declarative languages like SQL and Lex/Yacc use some elements of functional programming, such as not allowing mutable values. In addition, many other programming languages support programming in a functional style or have implemented features from functional programming, such as 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). print(len([2 + 1, 3 * 2, 1 / 0, 5 - 4])) fails under strict evaluation because of the division by zero in the third element of the list. Under lazy evaluation, the length function returns the value 4 (i.e., the number of items in the list), since evaluating it does not attempt to evaluate the terms making up the list. In brief, strict evaluation always fully evaluates function arguments before invoking the function. Lazy evaluation does not evaluate function arguments unless their values are required to evaluate the function call itself. The usual implementation strategy for lazy evaluation in functional languages is graph reduction. Lazy evaluation is used by default in several pure functional languages, including Miranda, Clean, and Haskell. argues for lazy evaluation as a mechanism for improving program modularity through separation of concerns, by easing independent implementation of producers and consumers of data streams. Launchbury 1993 describes some difficulties that lazy evaluation introduces, particularly in analyzing a program's storage requirements, and proposes an operational semantics to aid in such analysis. Harper 2009 proposes including both strict and lazy evaluation in the same language, using the language's type system to distinguish them. Consider C assignment statement x = x * 10 , this changes the value assigned to the variable x . Let us say that the initial value of x was 1 , then two consecutive evaluations of the variable x yields 10 and 100 respectively. Clearly, replacing x = x * 10 with either 10 or 100 gives a program a different meaning, and so the expression is not referentially transparent. In fact, assignment statements are never referentially transparent. Now, consider another function such as int plusOne(int x) { return x + 1; } is transparent, as it does not implicitly change the input x and thus has no such side effects. Functional programs exclusively use this type of function and are therefore referentially transparent. ===Data structures=== Purely functional data structures are often represented in a different way to their imperative counterparts. For example, the array with constant access and update times is a basic component of most imperative languages, and many imperative data-structures, such as the hash table and binary heap, are based on arrays. Arrays can be replaced by maps or random access lists, which admit purely functional implementation, but have logarithmic access and update times. Purely functional data structures have persistence, a property of keeping previous versions of the data structure unmodified. In Clojure, persistent data structures are used as functional alternatives to their imperative counterparts. Persistent vectors, for example, use trees for partial updating. Calling the insert method will result in some but not all nodes being created. int[] numList = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int result = 0; for (int i: numList) { if (i % 2 == 0) { result += i * 10; } } Functional programming with higher-order functions: import java.util.Arrays; int[] numList = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int result = Arrays.stream(numList) .filter(n -> n % 2 == 0) .map(n -> n * 10) .reduce(0, Integer::sum); Sometimes the abstractions offered by functional programming might lead to development of more robust code that avoids certain issues that might arise when building upon large amount of complex, imperative code, such as off-by-one errors (see Greenspun's tenth rule). (even? 5) (.equals (mod 5 2) 0) When benchmarked using the Criterium tool on a Ryzen 7900X GNU/Linux PC in a Leiningen REPL 2.11.2, running on Java VM version 22 and Clojure version 1.11.1, the first implementation, which is implemented as: (defn even? "Returns true if n is even, throws an exception if n is not an integer" {:added "1.0" :static true} [n] (if (integer? n) (zero? (bit-and (clojure.lang.RT/uncheckedLongCast n) 1)) (throw (IllegalArgumentException. (str "Argument must be an integer: " n))))) has the mean execution time of 4.76 ms, while the second one, in which .equals is a direct invocation of the underlying Java method, has a mean execution time of 2.8 μs – roughly 1700 times faster. Part of that can be attributed to the type checking and exception handling involved in the implementation of even? . For instance the lo library for Go, which implements various higher-order functions common in functional programming languages using generics. In a benchmark provided by the library's author, calling map is 4% slower than an equivalent for loop and has the same allocation profile, which can be attributed to various compiler optimizations, such as inlining. One distinguishing feature of Rust are zero-cost abstractions. This means that using them imposes no additional runtime overhead. This is achieved thanks to the compiler using loop unrolling, where each iteration of a loop, be it imperative or using iterators, is converted into a standalone Assembly instruction, without the overhead of the loop controlling code. If an iterative operation writes to an array, the resulting array's elements will be stored in specific CPU registers, allowing for constant-time access at runtime. For example, the function, mother(X) = Y, (every X has only one mother Y) can be represented by the relation mother(X, Y). Whereas functions have a strict input-output pattern of arguments, relations can be queried with any pattern of inputs and outputs. Consider the following logic program: mother(charles, elizabeth). mother(harry, diana). The program can be queried, like a functional program, to generate mothers from children: ?- mother(harry, X). X = diana. ?- mother(charles, X). X = elizabeth. But it can also be queried backwards, to generate children: ?- mother(X, elizabeth). X = charles. ?- mother(X, diana). X = harry. It can even be used to generate all instances of the mother relation: ?- mother(X, Y). X = charles, Y = elizabeth. X = harry, Y = diana. Compared with relational syntax, functional syntax is a more compact notation for nested functions. For example, the definition of maternal grandmother in functional syntax can be written in the nested form: maternal_grandmother(X) = mother(mother(X)). The same definition in relational notation needs to be written in the unnested form: maternal_grandmother(X, Y) :- mother(X, Z), mother(Z, Y). Here :- means if and , means and. However, the difference between the two representations is simply syntactic. In Ciao Prolog, relations can be nested, like functions in functional programming: grandparent(X) := parent(parent(X)). parent(X) := mother(X). parent(X) := father(X). mother(charles) := elizabeth. father(charles) := phillip. mother(harry) := diana. father(harry) := charles. ?- grandparent(X,Y). X = harry, Y = elizabeth. X = harry, Y = phillip. Ciao transforms the function-like notation into relational form and executes the resulting logic program using the standard Prolog execution strategy.
References
- ^ Berthe, Samuel (2024-04-29), samber/lo , retrieved 2024-04-29 Wikipedia revision 1366590619, retrieved 2026-11-21 · 1714345200.0
- ^ "Go Wiki: Compiler And Runtime Optimizations - The Go Programming Language" . go.dev . Retrieved 2024-04-29 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1714345200.0
- ^ "Comparing Performance: Loops vs. Iterators - The Rust Programming Language" . doc.rust-lang.org . Retrieved 2024-04-29 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1714345200.0
- ^ Hartel, Pieter; Henk Muller; Hugh Glaser (March 2004). "The Functional C experience" (PDF) . Journal of Functional Programming . 14 (2): 129– 135. doi : 10.1017/S0956796803004817 . S2CID 32346900 . Archived from the original (PDF) on 2011-07-19 . Retrieved 2006-05-28 . ; David Mertz. "Functional programming in Python, Part 3" . IBM developerWorks . Archived from the original on 2007-10-16 . Retrieved 2006-09-17 . ( Part 1 , Part 2 ) Wikipedia revision 1366590619, retrieved 2026-11-21 · 1148770800.0
- ^ "Functions — D Programming Language 2.0" . Digital Mars. 30 December 2012. Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ "Lua Unofficial FAQ (uFAQ)" . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ "First-Class Functions in Go - The Go Programming Language" . golang.org . Retrieved 2021-01-04 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1609714800.0
- ^ Eich, Brendan (3 April 2008). "Popularity" . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ van Rossum, Guido (2009-04-21). "Origins of Python's "Functional" Features" . Retrieved 2012-09-27 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1348700400.0
- ^ "functools — Higher order functions and operations on callable objects" . Python Software Foundation. 2011-07-31 . Retrieved 2011-07-31 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1312066800.0
- ^ Skarsaune, Martin (2008). The SICS Java Port Project Automatic Translation of a Large Object Oriented System from Smalltalk to Java . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Gosling, James. "Closures" . James Gosling: on the Java Road . Oracle. Archived from the original on 2013-04-14 . Retrieved 11 May 2013 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Williams, Michael (8 April 2013). "Java SE 8 Lambda Quick Start" . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Bloch, Joshua (2008). "Item 15: Minimize Mutability". Effective Java (Second ed.). Addison-Wesley. ISBN 978-0321356680 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ "Object.freeze() - JavaScript | MDN" . developer.mozilla.org . Retrieved 2021-01-04 . The Object.freeze() method freezes an object. A frozen object can no longer be changed; freezing an object prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties from being changed. In addition, freezing an object also prevents its prototype from being changed. freeze() returns the same object that was passed in. Wikipedia revision 1366590619, retrieved 2026-11-21 · 1609714800.0
- ^ Daniel Friedman; William Byrd; Oleg Kiselyov; Jason Hemann (2018). The Reasoned Schemer, Second Edition . The MIT Press. Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ A. Casas, D. Cabeza, M. V. Hermenegildo. A Syntactic Approach to Combining Functional Notation, Lazy Evaluation and Higher-Order in LP Systems. The 8th International Symposium on Functional and Logic Programming (FLOPS'06), pages 142-162, April 2006. Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ "How I do my Computing" . stallman.org . Retrieved 2024-04-29 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1714345200.0
- ^ Rodger, Richard (11 December 2017). The Tao of Microservices . Manning. ISBN 9781638351733 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Piro, Christopher (2009). Functional Programming at Facebook . CUFP 2009. Archived from the original on 2009-10-17 . Retrieved 2009-08-29 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1251500400.0
- ^ Momtahan, Lee (2009). Scala at EDF Trading: Implementing a Domain-Specific Language for Derivative Pricing with Scala . CUFP 2009. Archived from the original on 2009-10-17 . Retrieved 2009-08-29 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1251500400.0
- ^ Graham, Paul (2003). "Beating the Averages" . Retrieved 2009-08-29 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1251500400.0
- ^ Sims, Steve (2006). Building a Startup with Standard ML (PDF) . CUFP 2006 . Retrieved 2009-08-29 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1251500400.0
- ^ Laurikari, Ville (2007). Functional Programming in Communications Security . CUFP 2007. Archived from the original on 2010-12-21 . Retrieved 2009-08-29 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1251500400.0
- ^ Lorimer, R. J. (19 January 2009). "Live Production Clojure Application Announced" . InfoQ . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Bugnion, Pascal (2016). Scala for Data Science (1st ed.). Packt . ISBN 9781785281372 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ "The Elixir Programming Language" . Retrieved 2021-02-14 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1613257200.0
- ^ "Why developers like ClojureScript" . StackShare . Retrieved 2024-04-29 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1714345200.0
- ^ Herrick, Justin (2024-04-29), jah2488/elm-companies , retrieved 2024-04-29 Wikipedia revision 1366590619, retrieved 2026-11-21 · 1714345200.0
- ^ "Why developers like PureScript" . StackShare . Retrieved 2024-04-29 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1714345200.0
- ^ Team, Editorial (2019-01-08). "ALLEGRO - all you need to know about the best Polish online marketplace" . E-commerce Germany News . Retrieved 2024-04-29 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1714345200.0
- ^ "Websites using Phoenix Framework - Wappalyzer" . www.wappalyzer.com . Retrieved 2024-04-29 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1714345200.0
- ^ "Why Scheme for Introductory Programming?" . home.adelphi.edu . Retrieved 2024-04-29 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1714345200.0
- ^ Staff, IMACS (2011-06-03). "What Is Scheme & Why Is it Beneficial for Students?" . IMACS – Making Better Thinkers for Life . Retrieved 2024-04-29 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1714345200.0
- ^ de Moura, Leonardo; Ullrich, Sebastian (July 2021). "The Lean 4 Theorem Prover and Programming Language". Lecture Notes in Artificial Intelligence . Conference on Automated Deduction. Vol. 12699. pp. 625– 635. doi : 10.1007/978-3-030-79876-5_37 . ISSN 1611-3349 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Banz, Matt (2017-06-27). "An introduction to functional programming in JavaScript" . Opensource.com . Retrieved 2021-01-09 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1610146800.0
- ^ "Functional programming - Dlang Tour" . tour.dlang.org . Retrieved 2026-07-28 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1785193200.0
- ^ Functional Programming with C# - Simon Painter - NDC Oslo 2020 , 8 August 2021, archived from the original on 2021-10-30 , retrieved 2021-10-23 Wikipedia revision 1366590619, retrieved 2026-11-21 · 1634943600.0
- ^ Dominus, Mark J. (2005). Higher-Order Perl . Morgan Kaufmann . ISBN 978-1-55860-701-9 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Holywell, Simon (2014). Functional Programming in PHP . php[architect]. ISBN 9781940111056 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ "GopherCon 2020: Dylan Meeus - Functional Programming with Go" . YouTube . 22 December 2020. Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ "Functional Language Features: Iterators and Closures - The Rust Programming Language" . doc.rust-lang.org . Retrieved 2021-01-09 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1610146800.0
- ^ Vanderbauwhede, Wim (18 July 2020). "Cleaner code with functional programming" . Archived from the original on 28 July 2020 . Retrieved 6 October 2020 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Turing, A. M. (1937). "Computability and λ-definability". The Journal of Symbolic Logic . 2 (4). Cambridge University Press: 153– 163. doi : 10.2307/2268280 . JSTOR 2268280 . S2CID 2317046 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Haskell Brooks Curry; Robert Feys (1958). Combinatory Logic . North-Holland Publishing Company . Retrieved 10 February 2013 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Church, A. (1940). "A Formulation of the Simple Theory of Types". Journal of Symbolic Logic . 5 (2): 56– 68. doi : 10.2307/2266170 . JSTOR 2266170 . S2CID 15889861 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ McCarthy, John (June 1978). "History of LISP". The first ACM SIGPLAN conference on History of programming languages - HOPL-1 (PDF) . Los Angeles, CA. pp. 173– 185. doi : 10.1145/800025.808387 . {{ cite book }} : CS1 maint: location missing publisher ( link ) Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ John McCarthy (1960). "Recursive functions of symbolic expressions and their computation by machine, Part I." (PDF) . Communications of the ACM . 3 (4): 184– 195. doi : 10.1145/367177.367199 . S2CID 1489409 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Guy L. Steele; Richard P. Gabriel (February 1996). "The evolution of Lisp". History of programming languages---II (PDF) . pp. 233– 330. doi : 10.1145/234286.1057818 . ISBN 978-0-201-89502-5 . S2CID 47047140 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ The memoir of Herbert A. Simon (1991), Models of My Life pp.189-190 ISBN 0-465-04640-1 claims that he, Al Newell, and Cliff Shaw are "...commonly adjudged to be the parents of [the] artificial intelligence [field]," for writing Logic Theorist , a program that proved theorems from Principia Mathematica automatically. To accomplish this, they had to invent a language and a paradigm that, viewed retrospectively, embeds functional programming. Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Landin, Peter J. (1964). "The mechanical evaluation of expressions" . The Computer Journal . 6 (4). British Computer Society : 308– 320. doi : 10.1093/comjnl/6.4.308 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Diehl, Stephan; Hartel, Pieter; Sestoft, Peter (2000). "Abstract machines for programming language implementation". Future Generation Computer Systems . Vol. 16. pp. 739– 751. Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Landin, Peter J. (February 1965a). "Correspondence between ALGOL 60 and Church's Lambda-notation: part I" . Communications of the ACM . 8 (2). Association for Computing Machinery : 89– 101. doi : 10.1145/363744.363749 . S2CID 6505810 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Landin, Peter J. (March 1965b). "A correspondence between ALGOL 60 and Church's Lambda-notation: part II" . Communications of the ACM . 8 (3). Association for Computing Machinery : 158– 165. doi : 10.1145/363791.363804 . S2CID 15781851 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Landin, Peter J. (March 1966b). "The next 700 programming languages" . Communications of the ACM . 9 (3). Association for Computing Machinery : 157– 166. doi : 10.1145/365230.365257 . S2CID 13409665 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Backus, John (1978-08-01). "Can programming be liberated from the von Neumann style? a functional style and its algebra of programs" . Commun. ACM . 21 (8): 613– 641. doi : 10.1145/359576.359579 . ISSN 0001-0782 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ R.M. Burstall. Design considerations for a functional programming language. Invited paper, Proc. Infotech State of the Art Conf. "The Software Revolution", Copenhagen, 45–57 (1977) Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ R.M. Burstall and J. Darlington. A transformation system for developing recursive programs. Journal of the Association for Computing Machinery 24(1):44–67 (1977) Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ R.M. Burstall, D.B. MacQueen and D.T. Sannella. HOPE: an experimental applicative language. Proceedings 1980 LISP Conference, Stanford, 136–143 (1980). Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
- ^ Zhenjiang Hu and John Hughes and Meng Wang (2015). "How functional programming mattered". National Science Review . 2 : 349– 370. doi : 10.1093/NSR/NWV042 . Wikipedia revision 1366590619, retrieved 2026-11-21 · 1795215600.0
Cite this
"Functional programming". 1785272987. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1366590619