Talk:First-class functions: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Even C could?: Another informative link.)
(→‎Closures?: new section)
Line 43: Line 43:


::Thanks Dmitry. That is a valid point. I guess if a language has problems with the example but can reason, (making a better case than C), why it has first class functions, then they should make their case too. Maybe they could cover the four points by other means. --[[User:Paddy3118|Paddy3118]] 18:25, 24 February 2009 (UTC)
::Thanks Dmitry. That is a valid point. I guess if a language has problems with the example but can reason, (making a better case than C), why it has first class functions, then they should make their case too. Maybe they could cover the four points by other means. --[[User:Paddy3118|Paddy3118]] 18:25, 24 February 2009 (UTC)

== Closures? ==

Maybe this task is actually requiring [[wp:Closure (computer science)|closures]]? The concepts of first-class functions and closures are very inter-related and often confused. The ability to make a "compose" function requires closures, because the returned function needs to be able to remember ("close" around) the input functions when it is created so that it can use them to compute the correct result. When you talk about "creating" functions at run time, what you care about is the ability for it to close around run-time variables in scope at that time. Because if you couldn't do that, you would have what is morally equivalent to C function pointers, because they would be statically known at compile-time, and you could just "lift" them out of the functions that created them. --[[User:Spoon!|Spoon!]] 20:25, 24 February 2009 (UTC)

Revision as of 20:25, 24 February 2009

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)
wp:First-class_object makes good reading too. --Paddy3118 18:44, 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)
Thanks Dmitry. That is a valid point. I guess if a language has problems with the example but can reason, (making a better case than C), why it has first class functions, then they should make their case too. Maybe they could cover the four points by other means. --Paddy3118 18:25, 24 February 2009 (UTC)

Closures?

Maybe this task is actually requiring closures? The concepts of first-class functions and closures are very inter-related and often confused. The ability to make a "compose" function requires closures, because the returned function needs to be able to remember ("close" around) the input functions when it is created so that it can use them to compute the correct result. When you talk about "creating" functions at run time, what you care about is the ability for it to close around run-time variables in scope at that time. Because if you couldn't do that, you would have what is morally equivalent to C function pointers, because they would be statically known at compile-time, and you could just "lift" them out of the functions that created them. --Spoon! 20:25, 24 February 2009 (UTC)