Man or boy test: Difference between revisions

→‎F#: Add CPS version of straightforward approach.
imported>Arakov
imported>GLChapman
(→‎F#: Add CPS version of straightforward approach.)
 
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}}==
Anonymous user