Talk:First-class function: Difference between revisions

Content deleted Content added
Sectioning and indenting
Line 1:
{{WikiProject Computing|class=|importance=}}
==Analog in C==
"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 programming language|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. --[[User:MarkSweep|MarkSweep]] 07:08, 13 Nov 2004 (UTC)
----
 
::: 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. [[User:Wlievens|Wouter Lievens]] 21:40, 3 Apr 2005 (UTC)
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. --[[User:MarkSweep|MarkSweep]] 07:08, 13 Nov 2004 (UTC)
 
: 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. [[User:Wlievens|Wouter Lievens]] 21:40, 3 Apr 2005 (UTC)
 
== The notion of "dynamically creating functions" is grossly inaccurate. ==
Line 18 ⟶ 17:
I'd make these changes myself but I don't want to so majorly overhaul this node without some further input. <small>—The preceding [[Wikipedia:Sign your posts on talk pages|unsigned]] comment was added by [[Special:Contributions/74.128.168.38|74.128.168.38]] ([[User talk:74.128.168.38|talk]]) 22:36, 18 January 2007 (UTC).</small><!-- HagermanBot Auto-Unsigned -->
 
:I agree here. Maybe a mention of the eval() statement (or equivalents) would help to make the difference more obvious? --[[User:80.135.123.126|80.135.123.126]] 16:22, 26 January 2007 (UTC)
 
::I think that the definition as given here isn't really accurate. As far as I understand the term, the point is just that there is no difference between a function and a data type in the things you can do. This implies that you can assign local variables to functions, and then call those local variables in exactly the same way as you'd call a function. It implies that you can pass functions as arguments to other functions. Creating a function at runtime seems to me to be a completely different thing. In particular, you can achieve similar effects in every language I know of - even if you have to compile the code yourself into memory. I don't think that passing a function pointer around as in c, or wrapping the code in an inner class (java) or a Proc (ruby) qualifies as first-class functions. The point is that they are 'first-class' - that they don't need special treatment. [[User:88.96.214.6|88.96.214.6]] 10:25, 22 March 2007 (UTC)
 
: Clearly there are different interpretations of this term, so just listing the meaning you or I happen to be most familiar with is not going to produce an adequate article. It's better to make a clearer list of individual language features and discuss them separately. Features would at least include:
Line 28 ⟶ 27:
: I don't think an eval function, Lisp macros, C macros, C++ templates, code generation, or Java / .NET reflection are features that belong in this list. As remarked above it would be good to explicitly make this clear was well. [[User:Rp|Rp]] ([[User talk:Rp|talk]]) 08:17, 7 April 2008 (UTC)
 
==C has closures==
----
 
: 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:
Line 70 ⟶ 68:
: I must concur. [[User:Jsnx|jsnx]] ([[User talk:Jsnx|talk]]) 02:11, 7 December 2007 (UTC)
 
:: However you can access them by calling their containing module and asking for the function. Like:
----
 
: However you can access them by calling their containing module and asking for the function. Like:
<code>
def f(x) # global functions are contained in the Kernel
Line 84 ⟶ 80:
proc = g.to_proc # returns the method as a Proc
</code>
:: Also, in Ruby, a Proc is by definition exactly what a first class function is. Granted, you can't directly assign a method to a variable using the = operator like in other languages, I would still argue that Ruby does indeed have first class functions.
:: [[Special:Contributions/75.143.82.88|75.143.82.88]] ([[User talk:75.143.82.88|talk]]) 03:07, 3 August 2009 (UTC)
 
== Function literals in Python ==
Line 92 ⟶ 88:
 
<source lang="python">
 
 
 
>>> code="""def myfunc(a):