Functional programming: Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Eric Evers
mNo edit summary
imported>Eric Evers
mNo edit summary
Line 1: Line 1:
==Functional programming, time and state==
==Functional programming, time and state==


A functional programming language is a language modeled after mathematical functions. Non-functional computer programs allow  
A functional programming language is a language modeled after mathematical functions.  
state changes. State changes are represented by changes in the
Non-functional computer programs allow  
values of variables over time. In strict functional programming
state changes.  
languages variables are not allowed to change during a single
invocation of a function.


In strict functional programming state is not saved
First class data types
outside of function calls. By removing state outside of functions,
side effects are controled.


State may not change during a function call. Fixed state allows
Functional programs typically make functions first class data types. This
for the control of side effects(bad distant changes caused by local changes). When state is fixed, functions can be treated similar to
allows functions to be passes as arguments to other functions and
allows for the creation of higher order functions(functions of functions).
 
Input/Output
 
In functional programming state is not saved
outside of function calls due to restrictions on io(input:output calls).
State may not change during a function call. Restricting i/o
protects agaist side effects(bad distant changes(i/o) caused by local computation).
 
Single assignment and proofs
Single assignment makes a computation into a dataflow. Dataflows are easy for
compilers to understand. When state is fixed, functions can be treated similar to
mathematical functions and facts can be proved about such functions.
mathematical functions and facts can be proved about such functions.
Some functional programs can be proved to be equal to each other or
Some functional programs can be proved to be equal to each other or

Revision as of 12:53, 21 November 2008

Functional programming, time and state

A functional programming language is a language modeled after mathematical functions. Non-functional computer programs allow state changes.

First class data types

Functional programs typically make functions first class data types. This allows functions to be passes as arguments to other functions and allows for the creation of higher order functions(functions of functions).

Input/Output

In functional programming state is not saved outside of function calls due to restrictions on io(input:output calls). State may not change during a function call. Restricting i/o protects agaist side effects(bad distant changes(i/o) caused by local computation).

Single assignment and proofs

Single assignment makes a computation into a dataflow. Dataflows are easy for compilers to understand. When state is fixed, functions can be treated similar to mathematical functions and facts can be proved about such functions. Some functional programs can be proved to be equal to each other or proved to be stable under certain conditions. Such proofs can aid the efforts of software engineers and computer scientists. See (Hutton, 1999) for an example.

Ref 1: Graham Hutton, 1999, [1]

      A tutorial on the universality and expressiveness of fold
      J. Functional Programming 9 (4): 355–372, July 1999,     
      Cambridge University Press.

Some examples of functional programming languages are:

scheme, 
erlang, (excluding all parallel functions)
haskel.

If a functional language is eager then all values are fully computed in the order that they are encountered. In a lazy language like haskel some arguments are allowed to have delayed evaluation.