RosettaCodeData/Task/Y-combinator/SuperCollider/y-combinator.supercollider

40 lines
521 B
Plaintext

// z-combinator
(
z = { |f|
{ |x| x.(x) }.(
{ |y|
f.({ |args| y.(y).(args) })
}
)
};
)
// the same in a shorter form
(
r = { |x| x.(x) };
z = { |f| r.({ |y| f.(r.(y).(_)) }) };
)
// factorial
k = { |f| { |x| if(x < 2, 1, { x * f.(x - 1) }) } };
g = z.(k);
g.(5) // 120
(1..10).collect(g) // [ 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800 ]
// fibonacci
k = { |f| { |x| if(x <= 2, 1, { f.(x - 1) + f.(x - 2) }) } };
g = z.(k);
g.(3)
(1..10).collect(g) // [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ]