Headline

The language construct for anonymous functions according to the lambda calculus

Illustration

Suppose you want to increment all numbers in a list. Let us use Language:Haskell for illustration:

-- Sample input
sample = [1,3,5,7,11,13,17]
-- Expected output
expected = [2,4,6,8,12,14,18]

We shall use the map function to increment all elements of the input. We may apply the map function to a suitable increment function as follows:

-- Use a plain increment function
output = map inc sample
  where
    inc :: Int -> Int
    inc x = x + 1

Arguably, we may want to omit an explicit declaration of the increment function as its definition is trivial, not much insight can be gained from its definition, and no further reuse is intended (as we assume here). Thus, we may use an anonymous function instead of an extra increment function:

-- Use an anonymous increment function
output' = map (\x -> x + 1) sample

Thus, increment is expressed inline by an anonymous function that takes one argument x which is mapped to x + 1.

Arguably, a Haskell programmer may actually also consider the following form where addition is refined into increment by means of partial application, possibly even using Haskell's specific section notation:

-- Use partial application of (+) for increment
output'' = map ((+) 1) sample

-- Use a section for increment
output''' = map (+1) sample

All these attempts compute the expected result.


Ralf Lämmel edited this article at Mon, 03 Sep 2018 00:12:05 +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.