From Codidact with permission.
Description
APL trains are a series of functions, that get applied to an argument in this way:
(f g) x = f g x
here f
and g
are prefix functions
(f g h) x = (f x) g (h x)
here f
and h
are prefix functions, while g
is an infix function
(a b c d e f) x = (a (b c (d e f))) x = a (b x) c (d x) e (f x)
here f
, d
, b
, and a
are prefix functions, while e
and c
are infix functions
Trains evaluate from the right to the left, so in the last example, (f x)
is evaluated, then (d x)
, then (d x) e (f x)
, then (b x)
, etc.
For the purposes of this challenge, when counting from the right, the the first, third, fifth, etc. functions are monads, and the second, fourth, sixth, etc. functions are dyads, except that if the leftmost function would be a dyad, it is instead a monad because there is nothing to its left that can provide it with a left argument.
The final evaluation order there is fdebca
, or using numbers instead, 6 4 5 2 3 1
.
Challenge
Given a number n, output the evaluation order of a train with n functions. Your result can be 0 indexed or 1 indexed.
Examples
Here are the first 10 outputs starting from n=1 (1 indexed)
1 (0 if 0 indexed)
2 1 (1 0 if 0 indexed)
3 1 2
4 2 3 1
5 3 4 1 2
6 4 5 2 3 1
7 5 6 3 4 1 2
8 6 7 4 5 2 3 1
9 7 8 5 6 3 4 1 2
10 8 9 6 7 4 5 2 3 1
f
andg
in the first example monads? Sof
is applied to the result of applyingg
tox
? In the second example, isg
a dyad andf
,h
are monads? Sog
is applied to both the results off
andh
? Or how is(f x) g (h x)
interpreted. I think you should spell out the general rule. I for one cannot infer it from the examples (but maybe I will when I understand the notation) \$\endgroup\$