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 at 12:10 PM in Tools and Programming
[Trackback URL for this entry]

Or perhaps less pathologically: