2

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?

koteth
  • 35
  • 4
  • So, in other words, you want to know if a given list is sorted ascending? – Adám Jul 28 '21 at 07:08
  • Yes, but using the the formula I indicated. The formula is equivalent to a series of material implications connected by conjunction (if the list is in binary). a0 -> a1 AND a1->a2 etc.. – koteth Jul 28 '21 at 07:14
  • 1
    `∧/2≤/⊢` is a shorter form of your formula, but is there a reason you want to use that specific formula? – Adám Jul 28 '21 at 07:15
  • @Adám Thank you, I want to be able to write a formal representation of the formula. My goal would be to use apl as a compact tool for representing logical propositions. – koteth Jul 28 '21 at 07:27
  • OK, that makes sense. Then `∧/2≤/⊢` is strictly follows from your formula by the definition of dyadic `f/`. – Adám Jul 28 '21 at 07:29
  • 2
    Btw, while I encourage you to keep asking APL questions on SO, know that you're always welcome in [the APL chat room](https://chat.stackexchange.com/rooms/52405/the-apl-orchard) too. – Adám Jul 28 '21 at 07:30

2 Answers2

5

∧/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 ∧/

Adám
  • 6,573
  • 20
  • 37
  • Exactly what I was looking for (although the formulation as a ∧.≤ product would have more appeal) – koteth Jul 28 '21 at 07:45
1

⍳∘≢≡⍋

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?".

Adám
  • 6,573
  • 20
  • 37
  • Thanks but this is an equivalent form to the one I indicated, correct? I would like to calculate exactly and have a formal representation the operation (a0 ≤ a1) ∧ (a1 ≤ a2) ∧ .... ∧ (a[n-1] ≤ an) – koteth Jul 28 '21 at 07:18
  • @koteth The two will always give the same result on a real numeric list, however my solution will also work on e.g. complex numbers and higher-rank arrays, by using [total array ordering](https://apl.wiki/total_array_ordering). – Adám Jul 28 '21 at 07:21