Talk:First-class function: Difference between revisions

Content deleted Content added
Widged (talk | contribs)
links to partial implementation in js
Line 61:
 
Are these requirements met in Apple`s C extension ? If not,- then that C extension clearly does not introduced first-class functions. <span style="font-size: smaller;" class="autosigned">—Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/84.32.222.96|84.32.222.96]] ([[User talk:84.32.222.96|talk]]) 08:19, 22 February 2010 (UTC)</span><!-- Template:UnsignedIP --> <!--Autosigned by SineBot-->
 
== Does PHP really have first class functions? ==
 
So this is a classic "proof" of first class functions in php:
 
<code>
 
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
 
</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:
 
<code>
 
$cos = makeDerivative('sin', 0.00000001);
 
</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)
 
== Does Ruby really have first class functions? ==