Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"functions are first class values" what does this exactly mean?

Could somebody clearly explain it with some good example. I came across this statement in Scala, when explaining about the functional programming.

like image 368
Anil Avatar asked May 27 '12 21:05

Anil


People also ask

What is first class values in function?

In computer programming, a first-class object, first-class citizen, or first-class value is a language entity (e.g., function or variable) that operates as other entities in a language. For example, in the C programming language, you cannot pass a function to another function as a parameter.

What is meant by first class functions?

A programming language is said to have First-class functions when functions in that language are treated like any other variable. For example, in such a language, a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable.

What does it mean that functions are first class citizens in Scala?

In Scala, functions are first-class citizens because we can do the following things with them: We can use functions as values or like normal variables; we can replace a variable or value with a function. We can assign a function literal to a variable. We can pass one or more functions as another function's parameters.

Are functions first-class citizens?

Functions in JavaScript are first-class objects (or “first-class citizens”).


2 Answers

Being "first-class" is not a formally defined notion, but it generally means that an entity has three properties:

  1. It can be used, without restriction, wherever "ordinary" values can, i.e., passed and returned from functions, put in containers, etc.

  2. It can be constructed, without restriction, wherever "ordinary" values can, i.e., locally, in an expression, etc.

  3. It can be typed in a way similar to "ordinary" values, i.e., there is a type assigned to such an entity, and it can be freely composed with other types.

For functions, (2) particularly implies that a local function can use all names in scope, i.e. you have lexical closures. It also often comes with an anonymous form for construction (such as anonymous functions), but that is not strictly required (e.g. if the language has general enough let-expressions). Point (3) is trivially true in untyped languages.

So you see why functions in Scala (and in functional languages) are called first-class. Here are some other examples.

  • Functions in C/C++ are not first-class. While (1) and (3) are arguably available through function pointers, (2) is not supported for functions proper. (A point that's often overlooked.)

  • Likewise, arrays and structs are not first-class in C land.

  • Classes in Scala are not first-class. You can define and nest them, but not e.g. pass them to a function (only its instances). There are OO-languages with first-class classes, and in fact, the so-called nuObj calculus that informed Scala's design also allows that.

  • First-class modules are an often desired feature in ML-like languages. They are difficult, because they lead to undecidable type-checking. Some ML dialect allow modules to be wrapped up as first-class values, but arguably, that does not make modules first-class themselves.

like image 160
Andreas Rossberg Avatar answered Sep 20 '22 00:09

Andreas Rossberg


It means that functions can be passed around the same way as integers, sequences, etc.

An example (although not Scala):

>>> def add2(x):
...   return x + 2
... 
>>> map(add2, [1, 2, 3])
[3, 4, 5]
like image 45
Ignacio Vazquez-Abrams Avatar answered Sep 20 '22 00:09

Ignacio Vazquez-Abrams