Talk:First-class function: Difference between revisions

Content deleted Content added
Notification of altered sources needing review #IABot (v1.5.4)
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
Line 135:
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:
>>> 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:
And what about this? ->
 
<sourcesyntaxhighlight lang="python">
import math
 
Line 168:
# cos(math.pi/2) ~> 0.0
 
</syntaxhighlight>
</source>
 
== Regarding runtime generation ==
Line 220:
== 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 289:
 
In Python:
<sourcesyntaxhighlight lang="python">>>> def a(): pass
 
>>> def b(): pass
Line 300:
{<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:
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:
 
: For your peace of mind: Python has first-class functions and reference equality:
<sourcesyntaxhighlight lang="python">
>>> def main():
... a = 10
Line 348:
>>> (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:
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:
>>> 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:
>>> f()
4
>>> </sourcesyntaxhighlight> --[[User:Paddy3118|Paddy]] ([[User talk:Paddy3118|talk]]) 14:36, 17 February 2011 (UTC)
 
== Partial Function Application in C++ ==