Comments on Evaluation order of an APL n-train
Parent
Evaluation order of an APL n-train
Description
APL trains are a series of functions, that get applied to an argument in this way:
(f g) x = f g x
(f g h) x = (f x) g (h x)
(a b c d e f) x = (a (b c (d e f))) x = a (b x) c (d x) e (f x)
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.
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
BQN, 8 bytesSBCS ``` ⍒↕+2| …
3y ago
[Jelly], 8 bytes ṖUs2Uṭ …
3y ago
[Jelly], 5 bytes ḶHĊUỤ …
3y ago
[Haskell], 52 48 45 42 bytes …
3y ago
posix SH + [GNU sed], 43 bytes …
3y ago
Japt, 10 bytes o ÅÔò cÔ …
3y ago
[Husk], 8 bytes ηÖ↔mo⌈½ …
3y ago
Post
Haskell, 52 48 45 42 bytes
Caught mistake thanks to rak1507
Saved 3 bytes thanks to Hakerh400
f n=n:g[n-1,n-2..1]
g(b:c:t)=c:b:g t
g t=t
g
takes the rest of the trains.
-- This is a fork, so append c (monad) and b (dyad)
-- and continue with the rest of the train
g(b:c:t)#=[c,b]++g t
-- t is either empty or a single monad, so finish it off
g t=t
f
simply starts it off with the last function n
and the other trains 1..n-1
(in reverse).
f n=n:g[n-1,n-2..1]
1 comment thread