Y combinator: Difference between revisions

→‎{{header|SuperCollider}}: clarify why we need z to make the call lazy
m (syntax highlighting fixup automation)
(→‎{{header|SuperCollider}}: clarify why we need z to make the call lazy)
Line 5,115:
 
=={{header|SuperCollider}}==
 
Like Ruby, SuperCollider needs an extra level of lambda-abstraction to implement the y-combinator. The z-combinator is straightforward:
The direct implementation will not work, because SuperCollider evaluates x.(x) before calling f.
<syntaxhighlight lang="supercollider">
y = { |f| { |x| f.(x.(x)) }.({ |x| f.(x.(x)) }) };
</syntaxhighlight>
 
For lazy evaluation, this call needs to be postponed by passing a function to f that makes this call (this is what is called the z-combinator):
<syntaxhighlight lang="supercollider">// z-combinator
 
z = { |f| { |x| f.({ |args| x.(x).(args) }) }.({ |x| f.({ |args| x.(x).(args) }) }) };
 
// this can be also factored differently
(
zy = { |f|
{ |xr| xr.(xr) }.(
{ |x| f.({ |args| yx.(yx).(args) }) }
{ |y|
f.({ |args| y.(y).(args) })
}
)
};
)
 
// the same in a shorterreduced form
 
(
83

edits