Talk:First-class function

message: The language support table is excellent and reasonably detailed. However, it's missing the following languages:
* R 
* F# (under "Functional Languages")  — Preceding unsigned comment added by BryanFrazar (talk • contribs) 20:35, 6 January 2015 (UTC)
thread_title: Language Support table

message: "Most modern programming languages support functions defined statically at compile time. C additionally supports function pointers, which can be stored in data structures and passed as arguments to other functions. Nevertheless, C is not considered to support first class functions, since in general functions cannot be created dynamically during the execution of a program. The closest analog in C is that of a dynamically compiled function created by a just-in-time compiler, which is compiled as an array of machine language instructions in memory and then cast to a function pointer. However, this technique is specific to the underlying hardware architecture and is therefore neither general nor portable."
:I have problems with this.  The main thing is that the attempt to find an analog in C seems pointless to me.  I wouldn't try to find an analog of 'break' in Lisp, although I'm sure I could find something not too dissimilar if I strained hard enough.  In turn, I don't think it's conducive to understanding of the first class function to compare it to something like int (*f)().  Of course it is perfectly feasible to write C code to write a bit of C, compile it, put it into a dynamically linked library, open the library and execute it--in fact, this was even easier in BCPL, one of C's percursors, which had extensive dynamic module load and execution capabilities built into its standard library.  However this isn't really a first class function, it's just a neat programming trick.
::Well, I was trying to pre-empty a potential argument that the presence of function pointers in C is the same as having first class functions.  What are the crucial properties a function object has to have in order to qualify as first-class?  You can pass function pointers as arguments to other functions, and you can store them in memory and other data structures; in these respects function pointers and first class functions are indeed similar.  What distinguishes function pointers from first class functions is that the only values a function pointer can take on are the addressed of functions defined at compile/link time.  However, due to the presence of casts (nothing stops you from casting arbitrary pointers to function pointers), this is not literally true, but those casts are really only useful for in-memory compilation.  Since C directly supports function pointers and casts in the language, it makes sense to point out that these features don't add up to first class functions, with which they nevertheless share some properties. --MarkSweep 07:08, 13 Nov 2004 (UTC)
replies: text:  I agree with the garbage collector guy. When reading the page, you insintictively have the urge to say "hey but C...", yet the text corrects you in time. Wouter Lievens 21:40, 3 Apr 2005 (UTC)
thread_title: Analog in C

message: : If your using Apple's version of C then you can use their new block syntax which adds closures and runtime blocks to the C language. Apple has submitted their change to the C language to be added to the standard (its currently implemented on the the llvm-gcc and clang C compilers. These blocks/closures work in C, C++ and Objective-C 
: Here is an example of what they look like in C:
    void EvalFuncOnGrid( float(^block)(float) ) {
        int i;
        for ( i = 0; i < 5 ; ++i ) {
            float x = i * 0.1;
            printf("%f %f", x, block(x));
        }
    }
    
    void Caller(void) {
        float forceConst = 3.445;
        EvalFuncOnGrid(^float(float x){ return 0.5 * forceConst * x * x; });
    }
    
    void main(void) {
       Caller();
    }
: These blocks can be treated as first class functions, they have a dynamic binding, can be passed around at run time and automatically track references to variables used inside that are declared outside of their scope, thus they act as true closures. The complete specification can be found here: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1370.pdf
: 75.143.82.88 (talk) 03:29, 3 August 2009 (UTC)
replies: text: Yes, you can have a pointer to a block. https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/Blocks/Articles/bxOverview.html. ---- CharlesGillingham (talk) 01:08, 1 November 2014 (UTC)
thread_title: C has closures

message: So this is a classic "proof" of first class functions in php:
    function makeDerivative($fn, $deltaX) {
      return function($x) use ($fn, $deltaX) {
        return ($fn($x + $deltaX) - $fn($x)) / $deltaX;
      };
    }
    
    $cos = makeDerivative(sin, 0.00000001);
    echo $cos(0);        // 1
    echo $cos(pi() / 2); // 0
But there are a few things wrong with it.  First, this code actually throws a catchable fatal error.  The label 'sin' doesn't refer to the builtin function but rather the constant sin.  Since such a constant wasn't defined, php pretends you meant string 'sin'.  What you should actually do is:
    $cos = makeDerivative('sin', 0.00000001);
and for all intents and purposes, $fn in the closure is a string.  When you use $fn($x), php resolves the value of the string to some function and calls it with the arguments $x, but in no situation can you actually store or pass in a reference to a function.  The closure object is actually first class (you can pass it around, assign it to variables), but functions are not. 72.235.55.215 (talk) 09:36, 15 June 2012 (UTC)
thread_title:  Does PHP really have first class functions? 

message: IMO Python has unlimited function literals; it is possible to create any function (not limited to an expression like lambda functions) by creating a string and executing it; example:
>>> code="""def myfunc(a):
...  if isinstance(a, int):
...   print a**2
...  else:
...   print a, 'is not an integer number'
... """
>>> exec(code)
>>> myfunc(3)
9
>>> myfunc(3.0)
3.0 is not an integer number
>>>
(just tested using Python 2.5.2) --Tobias (talk) 09:55, 19 August 2008 (UTC)
thread_title:  Function literals in Python 

message: I just wanted to state agreement with the anonymous edit  removing the statement "A different set of difficulties arises when programs can create functions at runtime; if the program is compiled, this means that the runtime environment must itself include a compiler." Although it is possible for a program to generate another program in source or AST form and subsequently compile it at runtime, it is also possible for a program to directly generate machine code at runtime, and this is common in certain applications e.g. compiled bitmaps. Dcoetzee 12:30, 30 November 2008 (UTC)
thread_title:  Regarding runtime generation 

message: (see M.J. Dominus. Higher Order Perl, 2005. pp 325, 333)
$lambda = sub {$_[0] + 4};
print $lambda->(2), "\n"; #  => 6
Psilva (talk) 09:35, 26 September 2009 (UTC)
thread_title:  Higher-order functions in Perl 

message: I disagree with various statements concerning whether or not this is generally available in C - or that it must somehow require specific hardware.  Consider the following:
// imports first_class dll.
#include <first_class.h>
class dll_data;
class first_class;
main (char* args)
{
  dll_data *dll_data = new dll_data (int N, args);
  char *new_c_source;
  bool quit = false;
  while (!quit)
  {
  	first_class::load_dll ();	
        quit = first_class::run (dll_data);
	new_c_source = dll_data->new_source;
  	first_class::unload_dll ();
        if (new_c_source!=NULL)
	  first_class::recomplie_dll (dll_data->new_source);
  }
}
Windows actually has a "LoadModule" function (I think) that allows you to load dlls when you want them or need them, allowing you to do things like have one version of a program that can use different DLL's according to different OS, or whatever.  These can then be loaded during execution rather then when the program first starts up (automatically) and thus avoiding the dreaded "required DLL not found error".  The process can even be applied recursively, so that DLL's which have various complicated and interacting dependencies can be linked in and out, etc.  In my simple example I am suggesting a kind of persistent data block type (which could also be modified if needed by using placement new, casting to a void pointer, and/or realloc as needed).
Then there is the issue of "Active X" and so on.  I'm not sure how you would do this in Linux, although I am aware that Linux/Uxix variants to support some kind of _popen (char *filename -- etc) method which allows an application to create a new process and communicate with it via a named pipe and thus incur no more essential performance penalty other than whatever mighg be associated with named pipes.  Lots of things could be dynamically modified this way -- such as MPEG encoders, database engines, etc.  The pipes in such cases can be used for streaming the otherwise computationally intensive data -- even to the point that it becomes imaginable to create applications that could be run for months or even years (I have seen Linux boxes with uptime over 1 year)  —Preceding unsigned comment added by 67.116.237.73 (talk) 08:40, 5 December 2009 (UTC)
thread_title:  Higher-order functions in C/C++ 

message: I don't understand why C++ isn't listed as supporting partial application of functions.  This is exactly what the C++11 std::bind function does, and even in C++03 the std::bind1st and std::bind2nd functions provided for partial application, although in a limited way.  I've found a number of web pages (such as one from the Dream in Code web site) that support this view, but I don't know if any of them would be considered definitive.
63.232.147.98 (talk) 01:14, 22 February 2012 (UTC)
thread_title:  Partial Function Application in C++ 

message: Partial Application in JavaScript 
Partial function application in the functional js library.
--Widged (talk) 21:12, 13 March 2012 (UTC)
thread_title:  Partial Function Application in JavaScript 

message: Back in 2010 someone pointed out that Apple's C supports anonymous functions called "blocks". I don't know much about all the various versions of C, but Apple writes that "Blocks are available in GCC and Clang as shipped with the OS X v10.6 Xcode developer tools" (See iOS Developer Library:Blocks Programming Topics). So what version of C is this?
Blocks can assigned to variable, passed to functions, returned as values from functions. The block can refer to variables in the scope that they were defined and use persistent storage for the local variables if the function returns. In other words, I think they thought of everything as far as I can see. Thus this version of C has first class functions.
This info should be added to the table and the text, otherwise this article is in error.  ---- CharlesGillingham (talk) 01:19, 1 November 2014 (UTC)
replies: text: If this version of C has functions as well as blocks then no, the article is not in error - it is *not* called first class blocks. These versions of C would have blocks distinct from functions and so it would be correct to omit them. --Paddy (talk) 15:09, 12 November 2014 (UTC)
thread_title:  C has first-class functions (revisited) 

message: This language specific section does not have enough merit to be included as it only states the capabilities and limitations of one language and will only encourage a host of other language specific entries. It is better to delete it. --Paddy (talk) 21:45, 16 November 2014 (UTC)
replies: text: Agreed, but it may still be worth noting that in languages (e.g. Algol 68, C, C#) which treat functions as values (passing them to and returning them from functions, assigning them to variables and inside data structures) without being able to create new function bodies at runtime (lacking eval), higher order functions such as function composition can still be used to create new functions at runtime. Rp (talk) 22:42, 29 November 2014 (UTC)
thread_title:  Section "Case study: function composition" is not worth having. 

message: Language support for first class functions should not present a table grouping languages by family. It is not relevant to the topic but it is vague and at best arguable. Furthermore the specific taxonomy used is neither accurate nor intuitive with respect to first class functions. For example, one category is C Family and another is Scripting, while another is Functional. 
I suggest we remove the taxonomy from table and restructure the section.  — Preceding unsigned comment added by Rahab rx (talk • contribs) 18:36, 3 September 2016 (UTC)
replies: text: I disagree. Languages within a family typically have similar support for various language features. Simply ordering the table alphabetically or chronologically would make it a lot more messy. —Ruud 19:42, 6 September 2016 (UTC)
thread_title:  Language support breakdown of languages by taxonomy is misleading and irrelevant 

message: The source links to the functools library which includes functions common in functional programming. It does indeed have a partial application function, but what it does is just sort of provide a function that can be used in place of partial application. Python doesn't actually implement it on a syntax level. Have a function that takes an argument "x" and returns "x + 1" isn't partial application of "+1", it's just a function that is used instead of actual partial application. If Python has partial application, then every language which has closures also has partial application and you could probably argue that any language that has functions has partial application. 131.252.226.117 (talk) 19:23, 8 November 2016 (UTC)
thread_title:  I disagree that Python has partial application. 

message: Hello fellow Wikipedians,
I have just modified 2 external links on First-class function. Please take a moment to review my edit. If you have any questions, or need the bot to ignore the links, or the page altogether, please visit this simple FaQ for additional information. I made the following changes:
*Added archive https://web.archive.org/web/20120319071329/http://common-lisp.net/project/bknr/static/lmman/fd-clo.xml to https://common-lisp.net/project/bknr/static/lmman/fd-clo.xml
*Added archive https://web.archive.org/web/20110720102933/http://lambda.uta.edu/cse5317/l12.ppt to http://lambda.uta.edu/cse5317/l12.ppt
When you have finished reviewing my changes, you may follow the instructions on the template below to fix any issues with the URLs.
Cheers.—InternetArchiveBot (Report bug) 09:27, 1 October 2017 (UTC)
thread_title:  External links modified 

message: the first link in the first note leads to "page not found" — Preceding unsigned comment added by 2001:7c7:2051:195:e1ce:11c7:dd2e:fd8b (talk • contribs) 09:07, 27 September 2021 (UTC)
replies: text: Thanks, I fixed the link in the article. Johnuniq (talk) 11:04, 27 September 2021 (UTC)
thread_title:  link doesnt work