koteth
koteth

Reputation: 35

Check that each adjacent pair of a list is in order (tacit programming) in APL

Given a list in APL I would like to check that each adjacent pair is in order. So, given (a0, a1, ..., an), I would like to calculate:

(a0 ≤ a1) ∧ (a1 ≤ a2) ∧ .... ∧ (a[n-1] ≤ an)

I don't want to compute an equivalent form and I want to use tacit programming. My solution is ((¯1↓⊢)∧.≤(1↓⊢)) but it seems too verbose. Does anyone have any ideas?

Upvotes: 2

Views: 166

Answers (2)

Adám
Adám

Reputation: 7651

∧/2≤/⊢

X f/ Y computes the f-reductions in Y using sliding windows of size X. Therefore, if X←2 then we get pair-wise reductions, or in other words, insertion of f between subsequent pairs. is needed to complete the 3-train 2 ≤/ ⊢ and then we just have to AND together all the resulting Booleans using ∧/

Upvotes: 5

Adám
Adám

Reputation: 7651

⍳∘≢≡⍋

computes the grade and if the array is already sorted, then it will simply be an enumeration of the elements, so we use to compare the grade to ⍳∘≢ which is the indices for an array of the length that your array has.

You can find this on APLcart by searching for "sorted?".

Upvotes: 1

Related Questions