Talk:First-class functions

From Rosetta Code

Even C could?

Hi, please read the Wikipedia entry linked to, especially wp:First-class_function#Availability which specifically rules out C. I tried to make that point with my points about eval etc. --Paddy3118 18:15, 24 February 2009 (UTC)
  • Functions can be stored in other collection types of the language... yes (pointers)...
  • Functions can be used as arguments to other functions... yes (pointers)
  • Functions can be returned as the value of functions... yes (pointers)
  • New functions can be created from others at run time... maybe?

The last one is unclear to me. I can guess what it means, but every language (like C) can build a runtime that allows functions to be created from others at run time; you can build a library allowing that... meaning that with such a library C becomes a language with first-class functions...

I bet classifiers say that C functions are not first class... And if you can write such code, this does not mean that the language has first-class functions... so all these theoretical classifications regard only the primitive syntax (or better semantics?) of a language...? Or could I try to cheat? (Like I did for one-liner...!) --ShinTakezou 16:39, 24 February 2009 (UTC)

Yes, it's that last point that is crucial. In standard portable C, you can get a pointer to any function that was written in the original source code (of your program or a library), but there's no way to construct an arbitrary number of new functions.

You could simulate it by having a table and a set of functions referring to entries in it; but you would have to deal with allocating this limited resource. For example:

<lang c>typedef int (*ourfunc)(int);

ourfunc[4][2] composeData; int freeRow = 0;

int compose_0(int x) { return composeData[0][0](composeData[0][1](x)); } int compose_1(int x) { return composeData[1][0](composeData[1][1](x)); } int compose_2(int x) { return composeData[2][0](composeData[2][1](x)); } int compose_3(int x) { return composeData[3][0](composeData[3][1](x)); } ourfunc functions[4] = {compose_0, compose_1, compose_2, compose_3};

ourfunc compose(ourfunc a, ourfunc b) {

 composeData[freeRow][0] = a;
 composeData[freeRow][1] = b;
 return functions[freeRow++];

}</lang>

But this technique will only work for a fixed number of functions -- it is basically emulating closures by having a predefined table of closure entry points and data storage. --Kevin Reid 17:54, 24 February 2009 (UTC)

No, pointer to a function is only a first-class pointer. Its "first-classness" alone does not make first-class the thing it points to. The rest of your argumentation is about Turing completeness. Yes, you can create an object that will act similar to a function. A pointer to function is a simplest example of. But this would not make that object a function in language terms.
Some notes to the list. The list rather refers to operations on functional types. Neither of is actually required to make functions first-class. What is required is that a function were a type and there existed objects of this type, with some operations defined on this type. Whether these operations include any concrete operation beyond "call me," is up to the language. Only "call me" is essential (that makes the object a function). Granted, almost certainly a reasonable implementation will provide operations from the list. --Dmitry-kazakov 17:57, 24 February 2009 (UTC)