128 points · by lenmod · 15y ago · 36 comments
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.But when you drop the pretenses, you're still doing assignment. (Though otherwise you've perfectly duplicated the pattern.)
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);http://stackoverflow.com/questions/93526/what-is-a-y-combina...
I've never understood why y-combinator is useful or impressive. Interesting, maybe, but not really useful.
Although the Fexl language itself has syntax for recursive definitions, it translates everything to combinators internally, so there are no "symbol tables" or "environments" at run time. Therefore it uses the Y combinator to implement those recursive definitions.
A while ago I wrote this detailed example: http://news.ycombinator.com/item?id=2719635
tl;dr here is a non-recursive definition of the append function for two lists:
(Y \append \x\y x y \h\t cons h; append t y)
If you need more parentheses for clarity, here you go: (Y (\append \x\y x y \h\t cons h (append t y)))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
http://www.dsi.uniroma1.it/~labella/absMcAdam.ps (or search with Google for an HTML translation).
See http://www.paulgraham.com/noop.html for evidence that he isn't a big fan of OO.
People who see it and recognize it are the type of people he wants to catch attention from(hackers). Suits, would see it and ignore it.