Talk:First-class functions: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 67: Line 67:
:: ok, thanks. it does now. --[[User:Tinku99|Tinku99]] 17:49, 5 June 2009 (UTC)
:: ok, thanks. it does now. --[[User:Tinku99|Tinku99]] 17:49, 5 June 2009 (UTC)
So Autohotkey is parsing a string representation of the functions and eval-ing the result? If so then it gets the answer, but like C, wouldn't 'normally' be considered as having first class functions. --[[User:Paddy3118|Paddy3118]] 18:10, 5 June 2009 (UTC)
So Autohotkey is parsing a string representation of the functions and eval-ing the result? If so then it gets the answer, but like C, wouldn't 'normally' be considered as having first class functions. --[[User:Paddy3118|Paddy3118]] 18:10, 5 June 2009 (UTC)
:: AutoHotkey doesn't really support eval natively either. I just made a generic "compose" function, called map. It is overloaded to create a composition, and then funcall those compositions. AutoHotkey internal data structures include functions are documented in [http://www.autohotkey.com/forum/topic26300.html LowLevel] and can be created and manipulated as any other data. I think to do this in C, you just have to manipulate its internal data structures, which is machine code. See [[wp:Self-modifying_code]], Dynamic Machine Code Generation in [http://code.google.com/apis/v8/design.html v8 javascript] and AutoHotkey machine code functions [http://www.autohotkey.com/forum/topic21172.html script] --[[User:Tinku99|Tinku99]] 18:48, 5 June 2009 (UTC)

Revision as of 18:48, 5 June 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)
Maybe I am dumb or my english's weak, but it seems to me that wp:First-class_function#Availability does not rule out C, it only says that it is impractical (hard? slow? hardware-dependent?) to make the whole thing working in plain C... Anyway don't worry, my doubts were not about the way the task is specified, but about the way the task can be accomplished even in a language like C! :D And I am also very blind about too much abstraction and disagree with it is not possible to create new functions at runtime from wp:First-class_object; a stupid proof: I can write a python interpreter in C... (it all depends on what definitions make possible) --ShinTakezou 21:33, 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)

My bet was that writing codes, one can "simulate" it without the limit of fixed number of functions... but I am still thinking about it... --ShinTakezou 21:33, 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)
Chance there are that many languages at the end manipulate pointers to code; but this is hidden. In C, it is not so hidden, but I can feel it like a syntax oddness: a function is de facto a pointer to a function ---or the first-class implies also an uniform syntax to manipulate/use/refer to objects? If it is so, Python (and many more, and likely all!) could be out because e.g. sin means the function sin, but to "use" it I need the syntax sin(x)... Compilers/interpreters of languages that have first-class functions, must implement it someway, e.g. having something like a hash-table that "links" between names and pointers to object (which could be directly code or more complex data structure, depending on the context the language need to make its thing works)... So again the question: first-class functions is not about what you can do with the language, but about the syntax and what is already implemented? --ShinTakezou 21:33, 24 February 2009 (UTC)
It is about the syntax (i.e. the language), not about what is going on under the cover. To be first-class is a language property. So it obviously cannot be changed by means of writing a program. You must modify the language in order to make first-class things that are initially non-first-class. Regarding C++, I think it is quite close to have first-class functions because of <lang cpp>operator ()</lang>That does not give you closure or composition of functions, but as I said before that is unimportant. A first-class static functional object is still first-class object. We could compare it with a language that would not have no integers, only integer constants. In that language integer would be first-class, because you could have a constant integer object. Lacking integer assignment is nasty, but irrelevant. A bigger problem with C++ is that not all kinds of its functions can be implemented this way. For example, methods cannot be, only free functions. IMO, this qualifies C++ functions as non-first-class, not that they are static, not nested etc. --Dmitry-kazakov 08:42, 25 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)
I will try to think about how it can be done... but of course I would stop before implementing a full too-complex too-long runtime or a VM :D --ShinTakezou 21:33, 24 February 2009 (UTC)
I could not get away from the thought, when reading about First Class Functions, that the definition was a little hazy, and the only certainty was that if you had to do as much as you would have to in C, then you were doing too much to belong to the set of languages that could claim to support FCF. --Paddy3118 04:53, 25 February 2009 (UTC)
What a pity. "Syntax" constraints set by Dmitry-kazakov of course rule out C seriously this time (using preprocessor to extend syntax someway? :D too much effort anyway). The classification anyway is not clear. Dmitry says that it does not matter what happens under the "cover", and this makes it clearer, but it is also an interpretation of the definition (as stated). I.e. the definition explicitly prohibits the call for eval/exec, but it is obvious that a (belated) eval-uation is hidden behind the scene, e.g. the Python lambda could be "simulated" storing the piece of code into a string, specifying the dummy symbol, which will be changed to the real one when the "string" will be used, i.e. evaluated, against an argument... (I suppose internally it is done through local binding of the dummy symbol to the some thing which is bound to the argument, or something like that) ... --ShinTakezou 17:39, 25 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)

Don't merge with functional composition

functional composition is only part of the task. The idea is to show how well a language supports first-class functions with a task that needs more of the traits that the WP article says characterizes first class functions. --Paddy3118 10:03, 28 March 2009 (UTC)

Autohotkey problem

The Autohotkey entry is unfinished. Does what is there actually help? I don't know the language, so may be wrong about this second sentence. --Paddy3118 06:48, 4 June 2009 (UTC)

Please Clarify. What part is unfinished? Did you try running the example? Help what? --Tinku99 21:02, 4 June 2009 (UTC)

Hi Tinku, Autohotkey doesn't seem to create two lists: one of functions, the other of their matching inverses like the Javascript, Perl, Python, Ruby,... examples do? --Paddy3118 21:22, 4 June 2009 (UTC)

ok, thanks. it does now. --Tinku99 17:49, 5 June 2009 (UTC)

So Autohotkey is parsing a string representation of the functions and eval-ing the result? If so then it gets the answer, but like C, wouldn't 'normally' be considered as having first class functions. --Paddy3118 18:10, 5 June 2009 (UTC)

AutoHotkey doesn't really support eval natively either. I just made a generic "compose" function, called map. It is overloaded to create a composition, and then funcall those compositions. AutoHotkey internal data structures include functions are documented in LowLevel and can be created and manipulated as any other data. I think to do this in C, you just have to manipulate its internal data structures, which is machine code. See wp:Self-modifying_code, Dynamic Machine Code Generation in v8 javascript and AutoHotkey machine code functions script --Tinku99 18:48, 5 June 2009 (UTC)