Difference between revisions of "Functional programming/Alternative 1"

From HaskellWiki
Jump to navigation Jump to search
Line 39: Line 39:
 
subtractTwoFromList l = map (\x -> x - 2) l
 
subtractTwoFromList l = map (\x -> x - 2) l
 
</haskell>
 
</haskell>
Also, we can generalize this function to subtract any given number:
+
We can generalize this function to subtract any given number:
 
<haskell>
 
<haskell>
 
subtractFromList l y = map (\x -> x - y) l
 
subtractFromList l y = map (\x -> x - y) l
 
</haskell>
 
</haskell>
 
The function given to <hask>map</hask> then becomes a closure because <hask>\x -> x - y</hask> references a local variable (<hask>y</hask>) from the outside of its body.
 
The function given to <hask>map</hask> then becomes a closure because <hask>\x -> x - y</hask> references a local variable (<hask>y</hask>) from the outside of its body.
  +
  +
''Add reference to functional objects?''

Revision as of 16:27, 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

Features of functional languages

Higher-order functions

Higher-order functions are functions that take other functions as their arguments. Basic example of a HOF is map which takes a function and a list as its arguments, applies the function to all elements of the list and returns a list of results. For instance, we can write a function that subtracts 2 from all elements of a list without using loops or recursion:

subtractTwoFromList l = map (\x -> x - 2) l

We can generalize this function to subtract any given number:

subtractFromList l y = map (\x -> x - y) l

The function given to map then becomes a closure because \x -> x - y references a local variable (y) from the outside of its body.

Add reference to functional objects?