Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem here is that you are using a strictly evaluated programming language. The Y-combinator, pretty much like any other fixed point combinator, will only work properly when your functions are called by need, or 'lazily evaluated'.</p> <p>I know of a way to work around this (<a href="http://utrechthaskellcompiler.wordpress.com/2010/10/18/haskell-to-javascript-backend/">one of my professors looked into it a while ago</a>), but it will make your code completely unreadable.</p> <p>Below I've shown what's going on exactly, hoping you can see why JavaScript can't handle a straightforward implementation of SKI-calculus.</p> <hr> <p>This is what <code>Y</code> looks like after JavaScript evaluated your SKI-expression:</p> <pre><code>var Y = function (q) { return (function(p){return q(p(p))})(function(p){return q(p(p))}); }; </code></pre> <p>Now let's see what happens if you feed it your function <code>function (fac) { ... }</code>. Let's call that function <code>f</code>:</p> <pre><code>var factorial = (function(p){return f(p(p))})(function(p){return f(p(p))}); </code></pre> <p>Since the first anonymous function is applied to an argument, it will be evaluated into this:</p> <pre><code>var factorial = f( (function(p){return f(p(p))})(function(p){return f(p(p))}) ); </code></pre> <p>In a lazily evaluated language, the argument to <code>f</code> would now be left alone, and <code>f</code> itself would be evaluated. However, because JavaScript is a strictly evaluated language (or 'call-by-value'), it wants to know what argument it needs to pass to function <code>f</code> before actually running that function. So let's evaluate that argument, shall we?</p> <pre><code>var factorial = f(f( (function(p){return f(p(p))})(function(p){return f(p(p))}) ) ); </code></pre> <p>I guess now you're starting to see now where things go wrong, and how the Y-combinator actually works. In any case, your JavaScript machine will run out of stack space, because it's trying to build an infinite stack of calls to <code>f</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload