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 🙂

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;
}
```
• Demian says:

Nice casts!

But, Will It Blend?™

```int main(int a, char** f)
{
return f[1]?((int(*)(int,void*))f)(0,f):main(0,(char**)main);
}
```

Yes, it blends (no need for an extra function!): http://www.ideone.com/NYD5j

xD

• mchouza says:

Very impressive! I need to return to this blog, but Portal 2 is interfering… 😀