Purely functional programming
In computer science, purely functional programming usually designates a programming paradigm—a style of building the structure and elements of computer programs—that treats all computation as the evaluation of mathematical functions. Purely functional programing may also be defined by forbidding changing-state and mutable data.
Purely functional programing consists in restricting programming to its functional paradigm.
Difference between pure and not-pure functional programming
The exact difference between pure and impure functional programing is a matter of controversy.[1]
A program is usually said to be functional when it uses some concepts of functional programming, such as first-class functions and higher-order functions.[2] However, a first-class function may use techniques from the imperative paradigm, such as arrays or input/output methods are not purely functional programs, therefore the first-class function needs not be purely functional. In fact, the earliest programming languages cited as being functional, IPL and Lisp,[3][4] were both "impure" functional languages by the current definition.
Purely functional data structures 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 structures, while those data structures may not be used in purely functional programs.
Properties of purely functional program
Strict versus non-strict evaluation
All evaluation strategy which ends on a purely functional programs 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 than lazy evaluation. However, it is still possible that an eager evaluation may not terminate while the lazy evaluation of the same program halts.
Parallel computing
Purely functional programing simplifies parallel computing[5] since two purely functional parts of the evaluation never interact.
Data structures
Purely functional data structures are often represented in a different way than their imperative counterparts.[6] For example, array with constant-time access and update is a basic component of most imperative languages and many imperative data-structure, such as hash table 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 logarithmic. 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.
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
- ↑ Sabry, Amr (January 1993). "What is Purely Functional Language ?". J. Functional Programming. 8 (1): 1–22. doi:10.1017/S0956796897002943.
- ↑ Atencio, Luis (18 June 2016). Functional Programming in Javascript. Manning Publications. ISBN 978-1617292828.
- ↑ 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 which proved theorems from Principia Mathematica automatically. In order to accomplish this, they had to invent a language and a paradigm which, which viewed retrospectively, embeds functional programming.
- ↑ McCarthy, John (June 1978). "History of Lisp". In ACM SIGPLAN History of Programming Languages Conference: 217–223. doi:10.1145/800025.808387.
- ↑ Marlow, Simon (18 June 2013). Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming. O'Reilly Media. ISBN 978-1449335946.
- ↑ Purely functional data structures by Chris Okasaki, Cambridge University Press, 1998, ISBN 0-521-66350-4