Jump to content

Anonymous function

From Simple English Wikipedia, the free encyclopedia
Revision as of 19:18, 7 January 2013 by Kumioko (talk | changes) (Fix typos, dates, formatting, brackets and links. Replace contractions per What not to do, replaced: == Other pages == → ==Related pages== using AWB (8759))

In computer science and mathematics, an anonymous function is a function that has no name. Usually, a function is written like: . It can be written anonymously as These are used in functional programming languages and other programming languages with support for anonymous functions (those which support 1st class functions).

Examples in some programming languages

Python

(lambda x: (lambda y: x * y))

The above code can be executed as a function:

>>>(lambda x: (lambda y: x * y))(3)(4)
12

Haskell

\x -> \y -> x * y

The above code can be executed as a function:

(\x -> \y -> x * y) 3 4

The function can also be written in point-free (tacit) style:

(*)

Standard ML

Not curried:

fn (x, y) => x * y

(* or *)

(op * )

Curried:

fn x => fn y => x * y

JavaScript

// uncurried
function(x, y)
{
    return x * y;
}

// curried
function(x)
{
    return (function(y)
    {
        return x * y;
    });
}

Scheme

;; uncurried
(lambda (x y) (* x y))
*
;; usage:
((lambda (x y) (* x y)) 3 4)
(* 3 4)

;; curried:
(lambda (x) (lambda (y) (* x y)))

;; usage:
(((lambda (x) (lambda (y) (* x y))) 3) 4)

Other websites