

What is a y-combinator? - lenmod
http://stackoverflow.com/questions/93526/what-is-a-y-combinator

======
btilly
My favorite explanation remains the one I came up with at [http://www.mail-
archive.com/boston-pm@mail.pm.org/msg02716.h...](http://www.mail-
archive.com/boston-pm@mail.pm.org/msg02716.html).

~~~
Xurinos
Let's just pretend we are binding values. There are ways to make perl look
like you are doing parameter assignment.

    
    
        sub {
            my $builder = shift;
            return sub {
                my $n = shift;
                return $builder->($builder)->($n);
            };
        }->(
            sub {
                my $recurse = shift;
                return sub {
                    my $n = shift;
                    say "$n";  # Just a way to test
                    if (0 == $n) {
                        return 1;
                    }
                    else {
                        return $n * $recurse->($recurse)->($n - 1);
                    }
                };
           }
        )->(5);
    

I was confused about the "perl can't do this" part.

~~~
btilly
Pretending you are binding values instead of assignment means you can pretend
to achieve the goal in Perl.

But when you drop the pretenses, you're still doing assignment. (Though
otherwise you've perfectly duplicated the pattern.)

~~~
peteretep
I say I say I say...

    
    
      use strict; use warnings; use signatures;
    
      sub ( $builder ) {
    	return sub ( $n ) {
            return $builder->($builder)->($n);
        };
      }->(
    	sub ( $recurse ) {
    		return sub ( $n ) {
                if (0 == $n) {
                    return 1;
                } else {
    				return $n * $recurse->($recurse)->($n - 1);
            	}
            };
        }
      )->(5);

~~~
btilly
Cute. But I wrote the email under discussion in 2005, and the first version of
signatures was not released until 2008.

I still think that my email was accurate as of 2005.

------
speckledjim
Has anyone on HN ever actually used one of these in code they've written?

I've never understood why y-combinator is useful or impressive. Interesting,
maybe, but not really useful.

~~~
Homunculiheaded
I would say that the y-combinator is interesting to the such a degree that it
becomes useful. Sure you won't and shouldn't use the y-combinator in any real
world applications. But first off it demonstrates the true power of the lambda
calculus to represent computation, and more importantly to the practical
programmer is that understanding it will provide a deep insight into the
nature of computation, which is profoundly useful if you ask me.

~~~
fexl
It's a stretch to say that you _shouldn't_ use them in a real application.
Combinators in general can be useful for implementing functional programming
languages, and those languages can be useful for applications.

That may sound like bickering over a fine point, but I'm just saying that
combinators _can_ be more than a theoretical tool.

It seems reasonably fast for my purposes so far, and the expansion rule is
just a one-liner: <https://github.com/chkoreff/Fexl/blob/master/src/Y.c>

------
guelo
After you wrap your head around that barely useful, rarely used technique the
next question is, why did pg name his company after it?

~~~
rjbond3rd
He started a business, and now his business is starting other businesses.
Recursive...

~~~
guelo
Y Combinator is not simply recursion, and a function that creates other
functions isn't necessarily recursive anyway. He should have called it Factory
Pattern.

~~~
btilly
Paul Graham isn't a Java hacker, and isn't a fan of complex OO design
patterns. Naming it Factory Pattern would therefore be unlikely to appeal to
him.

See <http://www.paulgraham.com/noop.html> for evidence that he isn't a big fan
of OO.

~~~
Sandman
Just a small remark: Factory pattern is anything but complex. Y combinator
actually seems much more complex to me (though, I'm not proficient in
functional programming, so that might be why I find it complicated).

~~~
alnayyir
> (though, I'm not proficient in functional programming, so that might be why
> I find it complicated).

Hint.

------
sampsonjs
It's an opportunity for HN'ers to hold forth on programming.

~~~
SimHacker
FORTH ?KNOW IF HONK ELSE FORTH LEARN THEN

------
karmafeeder
Fixed.

