Headline

The functional programming paradigm

Illustration

Consider the following definition of the factorial function in Language:Haskell:

-- A recursive definition of the factorial function
factorial n =
  if n==0
    then 1
    else n * factorial (n-1)

This definition describes the computation of factorial in terms of basic arithmetic operations ("functions") and the recursive application of the factorial function itself. There are no variables that are assigned different values over time. This situation is representative of the functional programming paradigm.

For comparison, consider the following definition of the factorial function in Language:Java:

  // An imperative definition of the factorial function
  public static int factorial(int n) {
    int result = 1;
    for (int i=n; i>1; i--) 
      result = result * i;
    return result;
  }

A result variable is used in a loop to aggregate the product. Also, the loop uses a variable i to iterate from n down to 1. Arguably, the recursive formulation is also straightforward in Java, but Java with its emphasis on variables and assignment as well as mutable data structures and encapsulation of state in objects does not encourage functional programming.

Citation

(http://en.wikipedia.org/wiki/Functional_programming, 14 April 2013)

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. ... Functional programming has its roots in lambda calculus, a formal system developed in the 1930s to investigate computability, the Entscheidungsproblem, function definition, function application, and recursion. Many functional programming languages can be viewed as elaborations on the lambda calculus.


Ralf Lämmel edited this article at Mon, 01 May 2017 15:34:43 +0200
Compare revisions Compare revisions

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.