Difference between revisions of "Functional programming/Alternative 1"

From HaskellWiki
Jump to navigation Jump to search
(This article aims to be an improvement of "Functional programming")
 
m (typo)
Line 5: Line 5:
 
Many programming languages support programming in both functional and imperative styles, however each language has '''syntax''' and facilities that are optimised only for one of these styles. In addition to that, coding conventions and libraries often force the programmer to use one of the styles. Therefore, programming languages are divided into functional and imperative ones.
 
Many programming languages support programming in both functional and imperative styles, however each language has '''syntax''' and facilities that are optimised only for one of these styles. In addition to that, coding conventions and libraries often force the programmer to use one of the styles. Therefore, programming languages are divided into functional and imperative ones.
   
Following table shows which languages support functional programming (by supporting closures) and which for which the functional style is the dominant one.
+
Following table shows which languages support functional programming (by supporting closures) and for which the functional style is the dominant one.
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-

Revision as of 15:22, 21 December 2007

Functional programming means that programs are executed by evaluating expressions. This contrasts with imperative programming where programs are composed of statements which change global state when executed. Functional programming requires that functions are first-class, which means that they are treated like any other values and can be passed as arguments to other functions or be returned as a result of a function. Being first-class also means that it is possible to define and manipulate functions nested in code blocks. Special attention needs to be given to nested functions, called closures, that reference local variables from their scope. If such a function escapes their block after being returned from it, the local variables must be retained in memory as they might be needed lated when the function is called. Language implementations must contain special functionality to support this.

Functional vs imperative languages

Many programming languages support programming in both functional and imperative styles, however each language has syntax and facilities that are optimised only for one of these styles. In addition to that, coding conventions and libraries often force the programmer to use one of the styles. Therefore, programming languages are divided into functional and imperative ones.

Following table shows which languages support functional programming (by supporting closures) and for which the functional style is the dominant one.

Language Closures Functional
C/C++ No No
Java ? No
Python Yes No
Ocaml Yes Yes
Haskell Yes Yes