JS recursive puzzle

What happens when we execute the following Javascript code and why does it happen?

(function(f){f(f)})(function(g){g(g)})

In the next post, I will show the answer to this problem and the results of the subset sum collision detection. Stay tuned 🙂

Advertisements

8 thoughts on “JS recursive puzzle

  1. Fernando says:

    in order to understand recursivity, we need to understand recursivity, jejejeje

  2. lechuckGL says:

    Supongo que tarde o temprano tendria que tirar un stack overflow….eso, o crea una ruptura enel continuo espacio tiempo, destruyendo por completo esta dimension.

  3. Demian says:

    Today I tried to find an equivalent Groovy expression for this awesome magik. I think the result is pretty neat:

    ({it it}{it it})

    ‘{it it}’ is an anonymous function that receives one parameter, ‘it’, and invoques it passing itself as an argument. Note that parentheses are not needed for an invocation, so ‘it it’ is the same as ‘it(it)’. The same no-parentheses-needed trick is used to invoque the ‘{it it}’ passing an other, equivalent, function as a parameter. Finally, the surrounding parentheses are needed because the parser is not cool enough.

    An alternative, arguably clearer, and even shorter, equivalent version is:

    f={it it};f f

    But it lacks the awesome symmetry…

    It can even be further reduced to:

    f={it it}f

    But at that stage it’s just pure golfing =P

    • mchouza says:

      Interesting, I didn’t know that Groovy had such a concise syntax. I suppose it comes from its Ruby heritage.

      Here is the C version (run with ideone.com):

      void f(void *g)
      {
        ((void(*)(void*))g)(g);
      }
       
      int main(void)
      {
        f(f);
        return 0;
      }
      

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s