Mutual recursion: Difference between revisions
→{{header|jq}}: usual inner function
(Add BaCon) |
(→{{header|jq}}: usual inner function) |
||
Line 1,222:
=={{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
a function with the desired arity. 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:
▲ def F: if . == 0 then 1 else . - ((. - 1) | F | M) 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
Line 1,236 ⟶ 1,238:
[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))
|