Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Bill Clementson's Blog: JavaScript and Lisp (tech.coop)
31 points by r11t on Dec 11, 2009 | hide | past | favorite | 12 comments


Using JavaScript 1.8 expression closures:

  function Y(X)
      (function(procedure)
          X(function(arg) procedure(procedure)(arg)))
              (function(procedure)
                  X(function(arg) procedure(procedure)(arg)))


Nice but, anonymous recursion is possible with arguments.callee.



arguments.callee is deprecated in javascript 1.4. Use function name instead, so interpreter will have chance to perform tail recursion. Give names to your anonymous functions anyway: it will be much easier to trace them.

Example of anonymous recursive function with arguments.callee:

  var factorial=function(n) {
     return (!(n>1))? 1 : arguments.callee(n-1)*n;
  }
Example of anonymous recursive function without arguments.callee:

  var factorial=function factorial(n) {
     return (!(n>1))? 1 : factorial(n-1)*n;
 }


Tried Doug Crockford's little Javascripter: (define loop (lambda (x) (loop x))) (loop 1)

Result in Firefox: "Too much recursion"

I'm still surprised I didn't flunk B521 when Dan Friedman put something like this in my Java implementation of a Scheme interpreter and it blew up after some finite number of iterations.

It might have worked if method calls were properly tail-recursive!


Helped me find a cool video from this Waldemar Horwat guy. http://www.youtube.com/watch?v=Kq4FpMe6cRs


> will recognize that functions in JScript are fundamentally the Lambda Calculus

i would have thought you could say that about functions in any language...


Except languages where you can't pass functions as arguments or return them as results.


Horror! I didn't know such atrocities existed shudder ;)


you're right. i should have said "first class functions" (which narrows the field somewhat).


JavaScript is implementation of Scheme (dialect of Lisp) with Java-like syntax.


Or as I like to say "JavaScript is Scheme in C drag".




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: