Talk:First-class function: Difference between revisions

Content deleted Content added
syntaxhighlight & fix lint
 
(6 intermediate revisions by 6 users not shown)
Line 1:
{{WikiProject Computerbanner scienceshell|class=C|importance=high}}
{{WikiProject Computer science|importance=high}}
}}
{{To do}}
 
Line 41 ⟶ 43:
: 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:
<syntaxhighlight lang="C">
<code>
void EvalFuncOnGrid( float(^block)(float) ) {
int i;
Line 58 ⟶ 60:
Caller();
}
</syntaxhighlight>
</code>
: 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
: [[Special:Contributions/75.143.82.88|75.143.82.88]] ([[User talk:75.143.82.88|talk]]) 03:29, 3 August 2009 (UTC)
Line 75 ⟶ 77:
So this is a classic "proof" of first class functions in php:
 
<syntaxhighlight lang="php">
<code>
 
function makeDerivative($fn, $deltaX) {
Line 87 ⟶ 89:
echo $cos(pi() / 2); // 0
 
</syntaxhighlight>
</code>
 
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:
 
<syntaxhighlight lang="text">
<code>
 
$cos = makeDerivative('sin', 0.00000001);
 
</syntaxhighlight>
</code>
 
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. [[Special:Contributions/72.235.55.215|72.235.55.215]] ([[User talk:72.235.55.215|talk]]) 09:36, 15 June 2012 (UTC)
Line 102 ⟶ 104:
 
It seems to me that if you have to wrap a function in a 'proc' object in order to assign it to a variable, then your functions are second-class citizens. Something else I would expect to be able to do in a language that supports first class functions:
<syntaxhighlight lang="text">
<code>
 
def f(x)
Line 110 ⟶ 112:
g(2)
 
</syntaxhighlight>
</code>
 
This doesn't work in ruby either. You can't assign a function using = (the normal assignment operator), you have to use def, or wrap the function in an object. That's not first-class.[[User:88.96.214.6|88.96.214.6]] 12:26, 20 March 2007 (UTC)
Line 117 ⟶ 119:
 
:: However you can access them by calling their containing module and asking for the function. Like:
<syntaxhighlight lang="text">
<code>
def f(x) # global functions are contained in the Kernel
x + 4
Line 127 ⟶ 129:
g.class #=> 'Method'
proc = g.to_proc # returns the method as a Proc
</syntaxhighlight>
</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)
Line 135 ⟶ 137:
IMO Python ''has'' unlimited function literals; it is possible to create any function (not limited to an expression like <tt>lambda</tt> functions) by creating a string and executing it; example:
 
<sourcesyntaxhighlight lang="python">
 
>>> code="""def myfunc(a):
Line 148 ⟶ 150:
>>> myfunc(3.0)
3.0 is not an integer number
>>></sourcesyntaxhighlight>
 
(just tested using Python 2.5.2) --[[User:TobiasHerp|Tobias]] ([[User talk:TobiasHerp|talk]]) 09:55, 19 August 2008 (UTC)
Line 154 ⟶ 156:
And what about this? ->
 
<sourcesyntaxhighlight lang="python">
import math
 
Line 168 ⟶ 170:
# cos(math.pi/2) ~> 0.0
 
</syntaxhighlight>
</source>
 
== Regarding runtime generation ==
Line 220 ⟶ 222:
== Higher-order functions in Perl ==
(see M.J. Dominus. Higher Order Perl, 2005. pp 325, 333)
<sourcesyntaxhighlight lang="perl">
$lambda = sub {$_[0] + 4};
print $lambda->(2), "\n"; # => 6
</syntaxhighlight>
</source>
[[User:Psilva|Psilva]] ([[User talk:Psilva|talk]]) 09:35, 26 September 2009 (UTC)
 
Line 233 ⟶ 235:
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:
 
<syntaxhighlight lang="text">
<code>
 
// imports first_class dll.
Line 258 ⟶ 260:
}
 
</syntaxhighlight>
</code>
 
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).
Line 289 ⟶ 291:
 
In Python:
<sourcesyntaxhighlight lang="python">>>> def a(): pass
 
>>> def b(): pass
Line 300 ⟶ 302:
{<function a at 0x0000000003266A48>, <function b at 0x00000000032669C8>}
>>> type({a, b})
<class 'set'></sourcesyntaxhighlight>
 
: In Haskell the keys of a map need to be of a finite type in order to guarantee that the the equality function will terminate (or more pessimistically, you cannot even define and equality function for functions in Haskell). The function type is infinite. Does Python compare function pointers? And does it work in combination with anonymous functions? —''[[User:Ruud Koot|Ruud]]'' 07:46, 17 February 2011 (UTC)
 
<sourcesyntaxhighlight lang="python">>>> a = lambda : None
>>> b = lambda : None
>>> type(a)
Line 319 ⟶ 321:
False
>>> a is a
True</sourcesyntaxhighlight> --[[User:Paddy3118|Paddy]] ([[User talk:Paddy3118|talk]]) 08:36, 17 February 2011 (UTC)
 
It seems as if first classness of functions comes down to:
Line 335 ⟶ 337:
 
: For your peace of mind: Python has first-class functions and reference equality:
<sourcesyntaxhighlight lang="python">
>>> def main():
... a = 10
Line 348 ⟶ 350:
>>> (lambda x: x) == (lambda x: x)
False
</syntaxhighlight>
</source>
: —''[[User:Ruud Koot|Ruud]]'' 11:32, 17 February 2011 (UTC)
 
: Also Pythons nested functions seem crippled compared to Scheme:
<sourcesyntaxhighlight lang="python">
>>> def main():
... x = 1
Line 366 ⟶ 368:
File "<stdin>", line 4, in f
UnboundLocalError: local variable 'x' referenced before assignment
</syntaxhighlight>
</source>
: but only artificially and not fatally:
<sourcesyntaxhighlight lang="python">
>>> def main():
... x = [1]
Line 385 ⟶ 387:
>>> o()
5
</syntaxhighlight>
</source>
: —''[[User:Ruud Koot|Ruud]]'' 11:57, 17 February 2011 (UTC)
 
Hi Ruud, Python 3 addressed the access to outer variables issue with the nonlocal keyword:
<sourcesyntaxhighlight lang="python">Python 3.1 (r31:73572, Jun 28 2009, 18:34:47)
[GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
Type "copyright", "credits" or "license()" for more information.
Line 407 ⟶ 409:
>>> f()
4
>>> </sourcesyntaxhighlight> --[[User:Paddy3118|Paddy]] ([[User talk:Paddy3118|talk]]) 14:36, 17 February 2011 (UTC)
 
== Partial Function Application in C++ ==
Line 447 ⟶ 449:
== I disagree that Python has partial application. ==
 
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. [[Special:Contributions/131.252.226.117|131.252.226.117]] ([[User talk:131.252.226.117|talk]]) 19:23, 8 November 2016 (UTC)
 
== External links modified ==
 
Hello fellow Wikipedians,
 
I have just modified 2 external links on [[First-class function]]. Please take a moment to review [https://en.wikipedia.org/w/index.php?diff=prev&oldid=803238201 my edit]. If you have any questions, or need the bot to ignore the links, or the page altogether, please visit [[User:Cyberpower678/FaQs#InternetArchiveBot|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.
 
{{sourcecheck|checked=false|needhelp=}}
 
Cheers.—[[User:InternetArchiveBot|'''<span style="color:darkgrey;font-family:monospace">InternetArchiveBot</span>''']] <span style="color:green;font-family:Rockwell">([[User talk:InternetArchiveBot|Report bug]])</span> 09:27, 1 October 2017 (UTC)
 
== link doesnt work ==
 
the first link in the first note leads to "page not found" <!-- Template:Unsigned --><span class="autosigned" style="font-size:85%;">—&nbsp;Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[User:2001:7c7:2051:195:e1ce:11c7:dd2e:fd8b|2001:7c7:2051:195:e1ce:11c7:dd2e:fd8b]] ([[User talk:2001:7c7:2051:195:e1ce:11c7:dd2e:fd8b#top|talk]] • [[Special:Contributions/2001:7c7:2051:195:e1ce:11c7:dd2e:fd8b|contribs]]) 09:07, 27 September 2021 (UTC)</span>
:Thanks, I fixed the link in the article. [[User:Johnuniq|Johnuniq]] ([[User talk:Johnuniq|talk]]) 11:04, 27 September 2021 (UTC)