What is a first-class-citizen function?

A language that considers procedures to be “first-class” allows functions to be passed around just like any other value.

Languages like Java 7 (and earlier) and C “kind of” have this capability: C allows function pointers to be passed around, but you can’t dynamically define a function in those languages and suddenly pass that somewhere else. Java before version 8 can simulate this to a certain extent with anonymous classes, but it doesn’t technically have first-class functions.

On the other hand, C++, D, C#, Visual Basic .NET, Java 8+, and functional languages (like Scheme and Haskell) do allow you to pass around functions like variables. For example, the code below returns a function that adds addend to its input:

Written in D:

int delegate(int) makeAdder(int addend) //Returns a function
{
    return delegate int(int x) //Long way
    {
        return x + addend; //Notice that addend came from _outside_ the function
    };

    return (int x) { return x + addend; }; //Short way

    return x => addend + x; //Super-short way, introduced in D 2.058
}

Written in C#:

Func<int, int> MakeAdder(int addend) //Returns a function
{
    return delegate(int x) //The long way. Note: Return type is implicitly 'int'
    {
        return x + addend;
    };

    return x => x + addend; //Short way: x "goes to" (x + addend); inferred types
}

Written in C++:

#include <functional>

std::function<int(int)> make_adder(int addend)
{
    return [=](int x)
    {
        return addend + x;
    };
}

Written in Scala:

def makeAdder(addend: Int) = (x: Int) => addend + x

Written in Python:

def make_adder(addend):
    def f(x):
        return addend + x
    return f
    # or...
    return lambda x: addend + x

Written in Erlang:

make_adder(Addend) ->
    fun(X) -> Addend + X end.

Written in JavaScript:

function makeAdder(addend) {
    return function(x) {
        return addend + x;
    };
}

Written in JavaScript (ES2015 arrow function syntax):

const makeAdder = addend => x => addend + x;

Written in Scheme:

(define (makeAdder addend)
  (lambda (x)
    (+ x addend)))

Written in Haskell:

makeAdder :: Int -> (Int -> Int)
makeAdder addend = \x -> addend + x

Written in Visual Basic 2008:

Function MakeAdder(addend As Integer) As Func(Of Integer, Integer)
    Return Function(x) (x + addend)
End Function

Written in Swift (both verbose and short-hand implementations):

func makeAdder(append: Int) -> (x: Int) -> Int {
    return { (x: Int) -> Int in
        return x + append
    };
}

func makeAdder(append: Int) -> (Int) -> Int {
    return {$0 + append};
}

(By the way, a “lambda” is just a function without a name. Lambdas are only supported in languages that support first-class functions.)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)