Thursday, 21 August 2008

Lambda, the ultimate procrastination

« Javascript conventions | Main | Java semantics »
I was chatting with a friend about JavaScript's nice treatment of functions. I got a bit carried away writing an example inspired by such a construction I saw ages ago in Scheme by Kenneth Oksanen. The javascript is executable with rhino:
(function (foo) { return foo(foo,(function (foo) { return function() { return foo+=1;} ;})(1)); })(function (foo, baz) {return (function (prime) 
{print(prime); return foo(foo,(function(foo) {return function () {return foo(foo);};})(function (bar) {return (function(foo) {
return ((foo%prime) ? foo :bar(bar));})(baz());}));})(baz());});
Note that this should not be pasted into a browser as the "print" function has a different meaning in a browser (e.g. send it to a printer versus print it to standard out) and it will throw your browser into a hard to kill loop. I wrapped a version in a web page here which freezes my browser (firefox) for a few seconds but the browser eventually kills the script.
Posted by james at 12:10 PM in Tools and Programming

 

[Trackback URL for this entry]

Comment: James at Thu, 21 Aug 3:15 PM

Or perhaps less pathologically:


var factorial=((function(f) { return function (n) {return f(f,n);}})
(function (f,n) { return n == 0 ? 1 : n*f(f,n-1); }));

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« August »
SunMonTueWedThuFriSat
     12
3456789
10111213141516
17181920212223
24252627282930
31