Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

71%
+3 −0
Challenges Evaluation order of an APL n-train

BQN, 8 bytesSBCS ⍒↕+2|⊢+↕ Run online! The solution itself is an 8-train, so its evaluation order is given by the sequence 7 5 6 3 4 1 2 0: first the rightmost ↕, then the ⊢, then the + between...

posted 3y ago by Marshall Lochbaum‭

Answer
#1: Initial revision by user avatar Marshall Lochbaum‭ · 2021-06-07T23:19:16Z (almost 3 years ago)
# [BQN](https://mlochbaum.github.io/BQN/), 8 bytes<sup>[SBCS](https://github.com/mlochbaum/BQN/blob/master/commentary/sbcs.bqn)</sup>

```
⍒↕+2|⊢+↕
```

[Run online!](https://mlochbaum.github.io/BQN/try.html#code=VHIg4oaQIOKNkuKGlSsyfOKKoivihpUKCiMgQXBwbHkgdG8gMeKApjEwIGFuZCBhbGlnbiB2ZXJ0aWNhbGx5CuKJjcuYIFRywqggMSvihpUxMA==)

The solution itself is an 8-[train](https://mlochbaum.github.io/BQN/doc/train.html), so its evaluation order is given by the sequence `7 5 6 3 4 1 2 0`: first the rightmost `↕`, then the `⊢`, then the `+` between them, and so on—it's fully expanded below. Although it's organized as a train, the computation itself doesn't take advantage of BQN's support for trains.

```
⍒↕+2|⊢+↕  # Tacit function    (suppose 𝕩=3)
       ↕  # Range of 𝕩         0‿1‿2
     ⊢    # 𝕩, unchanged       3
      +   # Add                3‿4‿5
   2      #               2
    |     # Remainder mod ^    1‿0‿1
 ↕        # Range again        0‿1‿2
  +       # Add                1‿1‿3
⍒         # Descending grade   2‿0‿1
```

The idea is to start with ascending indices, then group them into mostly-twos by adding one to the first part of each pair. We do this by taking indices modulo 2. However, this means the first pair starts on element index 1—in english, the second element—which is correct for even lengths but wrong for odd lengths. So we flip the odd lengths by adding `𝕩` to the range. Subtracting would also work, in either order: perhaps a more intuitive version is `⊢-↕`, which is the distance from each position to one after the end.

After doing this we have a number for each function, and the ordering goes from high to low numbers but left to right for functions with the same number. Descending [Grade](https://mlochbaum.github.io/BQN/doc/order.html#grade) does exactly this.