Mutual recursion: Difference between revisions

jq
m (→‎vanilla: removed last blank line in program.)
(jq)
Line 904:
0,0,1,2,2,3,4,4,5,6,6,7,7,8,9,9,10,11,11,12</pre>
 
=={{header|jq}}==
 
jq supports mutual recursion but requires functions to be defined before they are used.
In the present case, this can be accomplished by first defining
a function with the desired arity. He we define F and M as arity-0 filters:
<lang jq>def F: 0; # declare required signature
 
def M: if . == 0 then 0 else . - ((. - 1) | M | F) end;
def F: if . == 0 then 1 else . - ((. - 1) | F | M) end;
</lang>Example:<lang jq>
[range(0;20) | F],
[range(0;20) | M]</lang><lang sh>$ jq -n -c -f Mutual_recursion.jq
 
[1,1,2,2,3,3,4,5,5,6,6,7,8,8,9,9,10,11,11,12]
[0,0,1,2,2,3,4,4,5,6,6,7,7,8,9,9,10,11,11,12]</lang>
=={{header|Julia}}==
<lang julia>F(n) = n < 1 ? one(n) : n - M(F(n - 1))
2,496

edits