Mutual recursion: Difference between revisions

Add ABC
(Add Refal)
(Add ABC)
 
(2 intermediate revisions by 2 users not shown)
Line 170:
m(0 - 19): 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 9, 9, 10, 11, 11, 12
</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO RETURN f n:
IF n=0: RETURN 1
RETURN n - m f (n-1)
 
HOW TO RETURN m n:
IF n=0: RETURN 0
RETURN n - f m (n-1)
 
WRITE "F:"
FOR n IN {0..15}: WRITE f n
WRITE /
 
WRITE "M:"
FOR n IN {0..15}: WRITE m n
WRITE /</syntaxhighlight>
{{out}}
<pre>F: 1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9
M: 0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9</pre>
 
=={{header|ACL2}}==
Line 890 ⟶ 910:
p 0.to(20).map! { n | female n }
p 0.to(20).map! { n | male n }</syntaxhighlight>
 
=={{header|Bruijn}}==
Normally it's not possible to call functions before they are defined. We can still induce mutual recursion using its [[Variadic_fixed-point_combinator|variadic fixed-point combinator]].
<syntaxhighlight lang="bruijn">
:import std/Combinator .
:import std/Number .
:import std/List .
 
f' [[[=?0 (+1) (0 - (1 (2 --0)))]]]
 
m' [[[=?0 (+0) (0 - (2 (1 --0)))]]]
 
f ^(y* (f' : {}m'))
 
m _(y* (f' : {}m'))
 
:test ((f (+0)) =? (+1)) ([[1]])
:test ((m (+0)) =? (+0)) ([[1]])
:test ((f (+4)) =? (+3)) ([[1]])
:test ((m (+4)) =? (+2)) ([[1]])
:test ((f (+15)) =? (+9)) ([[1]])
:test ((m (+15)) =? (+9)) ([[1]])
</syntaxhighlight>
 
=={{header|C}}==
Line 2,012 ⟶ 2,055:
for (i in 0..n) print("%3d".format(i))
println()
println("-".repeat(78(n + 2) * 3))
print("F :")
for (i in 0..24n) print("%3d".format(f(i)))
println()
print("M :")
for (i in 0..24n) print("%3d".format(m(i)))
println()
}</syntaxhighlight>
2,093

edits