Man or boy test: Difference between revisions

→‎F#: Add CPS version of straightforward approach.
(Add Zig example)
imported>GLChapman
(→‎F#: Add CPS version of straightforward approach.)
 
(2 intermediate revisions by 2 users not shown)
Line 1,402:
public program()
{
for(int n := 0,; n <= 14,; n += 1)
{
console.printLine(A(n,{^1},{^-1},{^-1},{^1},{^0}))
Line 1,605:
0
</syntaxhighlight>
 
F# supports tail calls (when compiled in Release mode), so you can also avoid stack overflow using the straightforward approach modified to use continuation-passing style:
<syntaxhighlight lang="fsharp">
[<EntryPoint>]
let main (args : string[]) =
let k = int(args.[0])
 
let l x cont = cont x
 
let rec a k x1 x2 x3 x4 x5 cont =
if k <= 0 then
x4 (fun n4 -> x5 (fun n5 -> cont (n4+n5)))
else
let mutable k = k
let rec b cont =
k <- k - 1
a k b x1 x2 x3 x4 cont
b cont
 
a k (l 1) (l -1) (l -1) (l 1) (l 0) (printfn "%d")
 
0
</syntaxhighlight>
 
 
=={{header|Fantom}}==
Line 3,763 ⟶ 3,787:
===CLI===
In Wren, a fiber's stack starts quite small but is then increased as needed, apparently limited only by available memory. Anyway, satisfying the test for k up to 20 is unlikely to be a problem on a modern machine.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var a
Line 3,809 ⟶ 3,833:
{{libheader|Wren-fmt}}
Based on Go's 'faster' version this runs in about 15.2 seconds which (surprisingly) is slightly faster than Go itself (16.6 seconds).
<syntaxhighlight lang="ecmascriptwren">/* man_or_boy_test_gmpMan_or_boy_test_2.wren */
 
import "./gmp" for Mpz
Anonymous user