carriagereturn

functional programming

Functional Programming

Functional programming is based on the idea of calculation. We define for ourselves the functions to be used and the implementation will calculate the value of expressions which use these functions, just as a traditional calculator will calculate the arithmetical expressions we give it.
Functional languages describe _what_ the answer is, whereas imperative language describe _how_ to get to the answer.

Examples of functional programming languages include Haskell and Miranda.

The three significant functions of a functional programming language are:

*map* : Takes a function and an array and returns an array which is the result of applying the function to each of the arguments in the initial array.
Example: map (*2) [1,2,3] = [2, 4, 6]

*fold* : Takes a binary (ie two arg) function and an array and returns a single value, the result of applying the function to each pair of elements in the array. You can fold to the right or the left.
Example: fold (+) [1,2,3]
= 1 + (fold (+) [2,3])
= 1 + 2 + (fold (+) [3])
= 1 + 2 + 3
= 6

*filter* : Take a single argument function that returns a boolean and and array and returns an array of those elements in the inital array for which the function returns true.
Example: filter (>2) [1,2,3] = [3]