Mutual recursion: Difference between revisions

Content added Content deleted
(Add BaCon)
(→‎{{header|jq}}: usual inner function)
Line 1,222: Line 1,222:


=={{header|jq}}==
=={{header|jq}}==

jq supports mutual recursion but requires functions to be defined before they are used.
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
In the present case, this can be accomplished by defining an inner function.
a function with the desired arity. He we define F and M as arity-0 filters:
<lang jq>def F: 0; # declare required signature


He we define F and M as arity-0 filters:
def M: if . == 0 then 0 else . - ((. - 1) | M | F) end;
<lang jq>
def F: if . == 0 then 1 else . - ((. - 1) | F | M) end;
def M:
</lang>Example:<lang jq>
def F: if . == 0 then 1 else . - ((. - 1) | F | M) end;
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) | F],
[range(0;20) | M]</lang><lang sh>$ jq -n -c -f Mutual_recursion.jq
[range(0;20) | M]</lang><lang sh>$ jq -n -c -f Mutual_recursion.jq
Line 1,236: Line 1,238:
[1,1,2,2,3,3,4,5,5,6,6,7,8,8,9,9,10,11,11,12]
[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>
[0,0,1,2,2,3,4,4,5,6,6,7,7,8,9,9,10,11,11,12]</lang>

=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>F(n) = n < 1 ? one(n) : n - M(F(n - 1))
<lang julia>F(n) = n < 1 ? one(n) : n - M(F(n - 1))