Fibonacci word: Difference between revisions

m (→‎{{header|jq}}: simplify)
(→‎{{header|jq}}: simplify)
Line 2,077:
</syntaxhighlight>
'''The task''':
<syntaxhighlight lang="jq"># Generate the first n terms of the Fibonacci word sequence
def enumerate(s): foreach s as $x (-1; .+1; [., $x]);
# as a stream of arrays of the form [index, word]
 
def fibonacci_words(n):
def fibonacci_words:
# input: [f(i-2), f(i-1), countdown, counter]
"1",
def fib:
(["0","1"]
if .[2] == 1 then [.[3], .[0]]
| recurse([add, .[0]])
else
(.[1] +| .[0]) as $sum;
 
| [ .[3], .[0]], ([ .[1], $sum, (.[2] - 1), (.[3] + 1) ] | fib)
# Generate the first n terms of the Fibonacci word sequence
end;
# as a stream of arrays of the form [index, word] starting with [0,1]
if n <= 0 then empty
def fibonacci_words($n):
else (["1", "0", n, 1] | fib)
enumerate(limit($n; fibonacci_words));
end;
 
def task(n):
2,472

edits