| Functional Programming |
Article Index for Functional |
Website Links For Functional |
Information AboutFunctional Programming |
| CATEGORIES ABOUT FUNCTIONAL PROGRAMMING | |
| programming paradigms | |
| functional programmingprogramming paradigms | |
| functional programming | |
| programming paradigms | |
| formal methods | |
|
Functional languages include APL , Erlang , Haskell , Lisp , ML , Oz and Scheme . Functional (symbolic math),2 Haskell , ML ,3 J and K (financial analysis), and Domain-specific Programming Language s like XSLT .45 The Lambda Calculus forms the foundation for most models of functional programming. HISTORY Lambda Calculus provides a theoretical framework for describing functions and their evaluation. Though it is a mathematical abstraction rather than a programming language, it forms the basis of almost all functional programming languages today. Combinatory Logic is an equivalent theoretical foundation, developed by Moses Schönfinkel and Haskell Curry . It was originally developed to achieve a clearer approach to the foundations of mathematics.6 Combinatory logic is commonly perceived as more abstract than Lambda Calculus and preceded it in invention. An early functional-flavored language was LISP , developed by John McCarthy while at MIT for the IBM 700/7000 Series scientific computers in the late 1950s.7 ''" The implementation of LISP began in Fall 1958."'' LISP introduced many features now found in functional languages, though LISP is technically a multi-paradigm language. Scheme and Dylan were later attempts to simplify and improve LISP. Information Processing Language (IPL) is sometimes cited as the first computer-based functional programming language. It is an assembly-style language for manipulating lists of symbols. It does have a notion of "generator", which amounts to a function accepting a function as an argument, and, since it is an assembly-level language, code can be used as data, so IPL can be regarded as having higher-order functions. However, it relies heavily on mutating list structure and similar imperative features. Kenneth E. Iverson developed the APL Programming Language in the early 1960s, described in his 1962 book "A Programming Language." APL was the primary influence on John Backus 's FP Programming Language . In the early 1990s, Iverson and Roger Hui created a successor to APL, the J Programming Language . In the mid 1990s, Arthur Whitney , who had previously worked with Iverson, created the K Programming Language , which is used commercially in financial industries. . Backus's paper popularized research into functional programming, though it emphasized Function-level Programming rather than the lambda-calculus style which has come to be associated with functional programming. In the 1970s the , the most common of which are now Objective Caml and Standard ML . The Haskell Programming Language was released in the late 1980s in an attempt to gather together many ideas in functional programming research. CONCEPTS A number of concepts and paradigms are specific to functional programming, and generally foreign to imperative programming (including Object Oriented Programming ). However, programming languages are often hybrids of several programming paradigms so programmers using "mostly imperative" languages may have utilized some of these concepts.8 Higher-order functions Functions are higher-order when they can take other functions as arguments, and return them as results. (The Derivative and Antiderivative in Calculus are examples of this). Higher-order functions are closely related to First-class Function s, in that higher-order functions and first-class functions both allow functions as arguments and results of other functions. The distinction between the two is subtle: "higher-order" describes a mathematical concept of functions that operate on other functions, while "first-class" is a computer science term that describes programming language entities that have no restriction on their use (thus first-class functions can appear anywhere in the program that other first-class entities like numbers can, including as arguments to other functions and as their return values). Higher-order functions enable Currying , a technique in which a function is applied to its arguments one at a time, with each application returning a new (higher-order) function that accepts the next argument. Pure functions Purely Functional programs have no Side Effect s. This makes it easier to reason about their behavior. For example, the result of applying a pure function to pure arguments does not depend on the order of evaluation. As a result, a language which has no impure functions (a "purely functional language," such as Haskell ) may use Call-by-need Evaluation . However, not all functional languages are pure. The Lisp family of languages are not pure because they allow side-effects. Since pure functions do not modify shared variables, pure functions can be executed in parallel without interfering with one another. Pure functions are therefore Thread-safe , which allow interpreters and compilers to use Call-by-future Evaluation . Pure functional programming languages typically enforce Referential Transparency , which is the notion that 'equals can be substituted for equals': if two expressions have "equal" values (for some notion of equality), then one can be substituted for the other in any larger expression without affecting the result of the computation. For example, in
a compiler can factor out f(x) if it is pure, transforming the program toz = f(x);
and eliminating the second evaluation of the (possibly costly) call to f(x). This optimization is called Common Subexpression Elimination .However, if a function has side effects, the function call cannot be eliminated. Consider the following program fragment:
The second call to random cannot be eliminated, because its return value may be different from that of the first call. Similarly,
cannot be optimized away; even if printf returns the same value both times; failing to make the second call would result in different program output.While most compilers for imperative programming languages detect pure functions, and perform common subexpression elimination for pure function calls, pre-compiled libraries generally do not expose this information, preventing calls to external functions from being optimized away. Some compilers, such as Gcc , add extra keywords for a programmer to explicitly mark external functions as pure so that this optimization can be performed in the presence of precompiled libraries. Fortran 95 allows functions without side-effects to be designated "pure". Recursion Iteration (looping) in functional languages is usually accomplished via Recursion . Recursive Function s invoke themselves, allowing an operation to be performed over and over. Recursion may require maintaining a stack, but Tail Recursion can be recognized and optimized by a compiler into the same code used to implement iteration in imperative languages. The Scheme Programming Language standard requires implementations to recognize and optimize tail recursion. Common patterns of recursion can be factored out using higher order functions, Catamorphism s and Anamorphism s (or "folds" and "unfolds") being the most obvious examples. Such higher order functions play a role analogous to built-in control structures such as loops in imperative languages. Strict, non-strict and lazy evaluation Functional languages can be categorized by whether they use strict or non-strict evaluation, concepts that refer to how function arguments are processed when an expression is being evaluated. To illustrate, consider the following two functions f and g: The following expression can be evaluated in one of two ways. Evaluate the innermost function g first: Or evaluate the outermost function f first: The first case is an instance of strict evaluation: arguments to a function are evaluated before the function call; while the second case is an instance of non-strict evaluation where arguments are passed to the function unevaluated and the calling function determines when the arguments are to be evaluated. Strict evaluation has efficiency advantages. An argument is evaluated once with strict evaluation, while it may be evaluated multiple times with non-strict evaluation, as can be seen in the above example where g(1,4) is evaluated twice. Also, strict evaluation is easier to implement since the arguments passed to a function are data values, whereas with non-strict evaluation arguments may be expressions, requiring some notion of closure. As a consequence the earliest functional languages, such as Lisp, ISWIM and ML, along with many of the modern functional languages use strict evaluation. However there are reasons for preferring non-strict evaluation. Lambda calculus provides a stronger theoretic foundation for languages that employ non-strict evaluation.9 Also non-strict evaluation provides for a more expressive language. For example it supports infinite data structures such as a list of all positive integers or all prime numbers. With non-strict evaluation these structures are only evaluated in contexts where a finite subset of the list is required. This led to the development of lazy evaluation, a type of non-strict evaluation where the results of an initial evaluation of any argument can be shared throughout the evaluation sequence. As a result an argument is never evaluated more than once. Lazy evaluation is used by the more modern, pure functional languages such as Miranda , Clean and Haskell . Functional programming in non-functional languages |
|
|