The symbol β½
indicates two different array transformations: with no left argument, it reverses the major cells of the array, but with a left argument, it rotates or cycles them around. These two possibilities, first put together in very early versions of APL, can't be considered restrictions or different views of some unifying function, but there are connections between them. Each returns an array with the same shape and all the same elements as π©
, possibly in a different arrangement. And elements that start out next to each other in π©
generally stay next to each otherβalways, if we consider an element on one edge to be next to the one opposite to it. One might think of them as isometries preserving a discrete subgroup of the torus, if one were inclined to think such things. On major cells, the two functions decompose the dihedral group okay I'll stop.
If there's no reason the data should be seen as cyclic or periodic, it's best to avoid Rotate: shift functions are probably more appropriate.
Reverse doesn't make things complicated. It puts the elements of a list the other way around, or more generally the major cells of an array.
βοΈβ½ "abcdefg" "gfedcba" β½ >"ab"βΏ"cd"βΏ"ef" ββ β΅"ef cd ab" β β½ 'c' Error: β½: Argument cannot be a unit
You can't reverse an atom or rank-0 array because it has no axes to reverse along, or it could be said no ordering to reverse.
To reverse along an axis other than the first, use Cells (Λ
) or Rank (β
).
β½Λ >"ab"βΏ"cd"βΏ"ef" ββ β΅"ba dc fe" β
Reverse is useful for folding left to right instead of right to left (here we use Pair to show structure).
βοΈβ Β΄ "abcd" # Right to left β¨ 'a' β¨ 'b' "cd" β© β© βΛΒ΄ β½ "abcd" # Left to right β¨ β¨ "ab" 'c' β© 'd' β©
Reverse is its own inverse β½βΌ
. So with Under, π½βΎβ½
reverses the argument, applies π½
, and reverses again. It's a particularly useful pattern with Scan, as it allows scanning from the end rather than the beginning of the array. For example, β¨`
on a list of booleans changes all bits after the first 1
to 1
, but β¨`βΎβ½
does this to all bits before the last 1
.
β¨` 0βΏ0βΏ1βΏ0βΏ0βΏ1βΏ0 β¨ 0 0 1 1 1 1 1 β© β¨`βΎβ½ 0βΏ0βΏ1βΏ0βΏ0βΏ1βΏ0 β¨ 1 1 1 1 1 1 0 β©
Rotate moves elements in a list around cyclically. It can also rotate any number of axes of the argument array by different amounts at once. That's discussed in the next section; for now we'll stick to a single number for π¨
. It has to be an integer, and π©
has to be an array with at least one axis.
2 β½ "rotate" "tatero" 2 (β’ β β½) 5βΏ2β₯"rotateCELL" ββ Β· ββ ββ β΅"ro β΅"te ta CE te LL CE ro LL" ta" β β β 2 β½ 'c' # No axes to rotate Error: β½: π© must have rank at least 1 for atom π¨
Elements are always rotated to the left, so that entry i
of the result is entry π¨+i
of the argumentβor rather, entry (β π©)|π¨+i
to enable elements to cycle around. This can be seen directly by using the range βn
as an argument: then the value of π©
at index i
is just i
.
2 β½ β6 β¨ 2 3 4 5 0 1 β©
The rotation (β π©)β½π©
moves each element the entire length of π©
, which just places it back where it started. In fact, adding β π©
to the rotation amount never changes the behavior or the rotation. In terms of indices, this is because (β π©)|(β π©)+a
is a
.
To rotate the other way, use a negative left argument (so -βΈβ½
is a simple way to write "reverse rotate"). This will always be the same as some leftwards rotation, since (-r)β½π©
is ((β π©)-r)β½π©
, but could be more convenient.
Β―2 β½ "rotate" "terota"
The easiest way to rotate along a later array axis is usually to use the Cells (Λ
) or Rank (β
) modifier.
β’ tab β 3βΏ4β₯"abcdABCD0123" ββ β΅"abcd ABCD 0123" β 1 β½Λ tab # Rotate the second axis ββ β΅"bcda BCDA 1230" β
Rotate also allows π¨
to be a list (or unit array) of integers, in which case they're matched with leading axes of π©
. This means the length of π¨
can't be larger than the rank of π©
, or there wouldn't be enough axes to match. This rule that π©
has to have rank one or more when π¨
is an atom is a special case, because then π¨
is treated as the one-element list β₯π¨
.
3βΏ4βΏ2 β½ "just a list" Error: β½: Length of list π¨ must be at most rank of π© (3 β‘ β π¨, β¨11β© β‘ β’π©β©
The expression below rotates the first (vertical) axis of tab
by one element, and second by two. So the line of capital letters goes from being one away from the top, up to the top, and the column with '2'
goes from horizontal index 2 to index 0.
1βΏ2 β½ tab ββ β΅"CDAB 2301 cdab" β
The vertical and horizontal rotations are independent, and could also be done with two β½
s and a Λ
. The multi-axis form is more convenient, and can potentially be evaluated faster than multiple separate rotations in the cases where it shows up.
1 β½ 2 β½Λ tab ββ β΅"CDAB 2301 cdab" β