Variadic fixed-point combinator: Difference between revisions

Have removed my own post as I misunderstood the task.
(Have removed my own post as I misunderstood the task.)
Tag: Manual revert
 
(9 intermediate revisions by 5 users not shown)
Line 60:
:test (odd? (+5)) ([[1]])
</syntaxhighlight>
 
=={{header|F Sharp|F#}}==
The following uses [[Y_combinator#March_2024]]
<syntaxhighlight lang="fsharp">
// Variadic fixed-point combinator. Nigel Galloway: March 15th., 2024
let h2 n = function 0->2 |g-> n (g-1)
let h1 n = function 0->1 |g->h2 n (g-1)
let h0 n = function 0->0 |g->h1 n (g-1)
let mod3 n=Y h0 n
[0..10] |> List.iter(mod3>>printf "%d "); printfn ""
</syntaxhighlight>
{{output}}
<pre>
0 1 2 0 1 2 0 1 2 0 1
</pre>
=={{header|FreeBASIC}}==
Unfortunately, due to the limitations of the FreeBASIC language, implementing a variadic fixed-point combinator without explicit recursion is extremely difficult, if not impossible. FreeBASIC does not support higher-order functions in a way that allows this type of metaprogramming.
 
An alternative would be to implement recursion explicitly, although this does not satisfy your original requirement of avoiding explicit recursion.
<syntaxhighlight lang="vbnet">Declare Function Even(n As Integer) As Integer
Declare Function Odd(n As Integer) As Integer
 
Function Even(n As Integer) As Integer
If n = 0 Then Return 1
Return Odd(n - 1)
End Function
 
Function Odd(n As Integer) As Integer
If n = 0 Then Return 0
Return Even(n - 1)
End Function
 
Function Collatz(n As Integer) As Integer
Dim As Integer d = 0
While n <> 1
n = Iif(n Mod 2 = 0, n \ 2, 3 * n + 1)
d += 1
Wend
Return d
End Function
 
Dim As String e, o
Dim As Integer i, c
For i = 1 To 10
e = Iif(Even(i), "True ", "False")
o = Iif(Odd(i), "True ", "False")
c = Collatz(i)
Print Using "##: Even: & Odd: & Collatz: &"; i; e; o; c
Next i
 
Sleep</syntaxhighlight>
{{out}}
<pre>Similar to Julia/Python/Wren entry.</pre>
 
=={{header|Haskell}}==
Line 120 ⟶ 173:
 
=={{header|Phix}}==
Translation of Wren/Julia/JavaScript/Python... The file closures.e was added for 1.0.5, which has [not yet shipped], and haswith the somewhat non-standard requirement of needing any captures explicitly stated [and returned if updated], and invokable only via the [new] call_lambda() function, rather thannot directlydirect or the usual[implicit] call_func() or the implicit invocation of that. Not yet properly documented, or for that matter fully tested...<br>
Disclaimer: Don't ask me if this is a proper Y combinator, all I know for sure is it converts a set of functions into a set of closures, without recursion.
<syntaxhighlight lang="phix">
include builtins/closures.e -- auto-include in 1.0.5+ (needs to be manually installed and included prior to that)
Line 143 ⟶ 197:
end function
 
function c1(sequence f3f, integer n, d)
if n=1 then return d end if
return call_lambda(f3f[2+odd(n)],{n,d+1})
end function
 
function c2(sequence f3f, integer n, d)
return call_lambda(f3f[1],{floor(n/2),d})
end function
 
function c3(sequence f3f, integer n, d)
return call_lambda(f3f[1],{3*n+1,d})
end function
 
Line 183 ⟶ 237:
10: even:true, odd:false, collatz:6
</pre>
 
=={{header|Python}}==
A re-translation of the Wren version.
<syntaxhighlight lang="python">Y = lambda a: [(lambda x: lambda: x(Y(a)))(f) for f in a]
 
even_odd_fix = [
lambda f: lambda n: n == 0 or f[1]()(n - 1),
lambda f: lambda n: n != 0 and f[0]()(n - 1),
]
 
collatz_fix = [
lambda f: lambda n, d: d if n == 1 else f[(n % 2)+1]()(n, d+1),
lambda f: lambda n, d: f[0]()(n//2, d),
lambda f: lambda n, d: f[0]()(3*n+1, d),
]
 
even_odd = [f() for f in Y(even_odd_fix)]
collatz = Y(collatz_fix)[0]()
 
for i in range(1, 11):
e = even_odd[0](i)
o = even_odd[1](i)
c = collatz(i, 0)
print(f'{i :2d}: Even: {e} Odd: {o} Collatz: {c}')
</syntaxhighlight>
 
 
 
=={{header|Wren}}==
871

edits