Functional programming: Difference between revisions

From Rosetta Code
Content added Content deleted
(Reorganized article, added some references)
m (Added TR link)
Line 7: Line 7:
* It's easy to '''refactor''' similar pieces of code, because any subexpression can be replaced by a variable bound at the outside.
* It's easy to '''refactor''' similar pieces of code, because any subexpression can be replaced by a variable bound at the outside.


This leads to a coding style which is very different from more traditional languages. [[Iteration]] is typically replaced by tail-[[recursion]]. Lists become a very important datatype. Code often consists of a composition of simpler blocks, which makes it look similar to [[declarative programming]].
This leads to a coding style which is very different from more traditional languages. [[Iteration]] is typically replaced by [[tail recursion|tail-recursion]]. Lists become a very important datatype. Code often consists of a composition of simpler blocks, which makes it look similar to [[declarative programming]].


Most functional programming languages (FPLs) are related to the [[wp:Lambda_Calculus|lambda calculus]], which makes the specification of their formal semantics simpler.
Most functional programming languages (FPLs) are related to the [[wp:Lambda_Calculus|lambda calculus]], which makes the specification of their formal semantics simpler.

Revision as of 20:26, 1 October 2008

Functional programming treats functions as the fundamental first-class objects of the programming language. That has several consequences:

This leads to a coding style which is very different from more traditional languages. Iteration is typically replaced by tail-recursion. Lists become a very important datatype. Code often consists of a composition of simpler blocks, which makes it look similar to declarative programming.

Most functional programming languages (FPLs) are related to the lambda calculus, which makes the specification of their formal semantics simpler.

One important characteristic of FPLs is their default evaluation order. Strict or eager FPLs will evaluate an argument as soon as possible, while lazy evaluation will do that as late as possible.

Strict FPLs often have an impure aspect that does allow functions with implicit side-effects, in order to interact with the (stateful) outside world. In non-strict FPLs, one uses monads or other means like uniqueness types to guarantee correct sequencing of side-effects.

With monads, one can also do imperative programming even in a purely functional languages, which is especially helpful if the notion of state is natural to the problem space.