## Sunday, June 13, 2021

### Reading APL #1: Roman numerals to decimal

As part of a new recurring series I’m going to take a small APL function or program and try to break it down in to its parts. I hope this is going to help me get a better understanding of the language.

Order of evaluation: Expressions in APL are evaluated right-to-left, and there is no hierarchy of function precedence

Two additional concepts are useful to understand when trying to read code:

1. A monadic function : an operation applied to the right expression
2. A dyadic function : a function applied to two arguments ( left and right )

One example of these elements is the following:

``````      2 2 ⍴⍳⍴ 8 8 8 8
1 2
3 4``````

Here is an example of evaluating this expression from right to left.

``````      myarray←8 8 8 8
myarray
8 8 8 8
length_of_my_array← ⍴ myarray
length_of_my_array
4
new_array← ⍳ length_of_my_array
new_array
1 2 3 4
2 2 ⍴ new_array
1 2
3 4``````

Note that here we use `⍴` both as a monadic function (Shape) and as a dyadic function (Reshape).

## The example

For this post I’m going to use a small function I found in the APL Utils repository by Blake McBride . This function takes a string representing a roman number and converts it back to an integer:

``````∇z←Romanu a
z←+/(¯1+2×z≥1↓z,0)×z←(1 5 10 50 100 500 1000)['IVXLCDM'⍳a]
∇``````

An example of executing this function:

``````      Romanu 'XVII'
17
Romanu 'IX'
9``````

Let’s start by reading the function definition:

``````∇z←Romanu a
...
∇``````

This code represents the definition of the `Romanu` function with an `a` argument.

Now, the interesting part is reading the body of the function which performs the actual calculations.

``z←+/(¯1+2×z≥1↓z,0)×z←(1 5 10 50 100 500 1000)['IVXLCDM'⍳a]``

As described in the previous section we need to read this code from right to left. To do this we need to ‘parse’ it and extract the largest complete expression from the right:

``(1 5 10 50 100 500 1000)['IVXLCDM'⍳a]``

This expression is a particular example of an bracked indexing expression(https://aplwiki.com/wiki/Bracket_indexing). A simpler example of this is:

``````      (10 20 30)
10
(10 20 30)
20
(10 20 30)
30
(10 20 30)[3 1 1 2]
30 10 10 20
a←10 20 30
a[3 1 1 2]
30 10 10 20``````

As shown above this expression is used to select elements of an array into a new array.

In the `Romanu` function this expression is used to assign a value to eacho of the roman numeral 'digits':

``'IVXLCDM'⍳a``

Here the dyadic form of `⍳` or ‘Index of’(https://aplwiki.com/wiki/Index_Of) . This expression returns the indices of the left side array of the elements on the right. Here are some examples of this expression in action:

``````      (10 20 30) ⍳ (20 20 10 20 30 10)
2 2 1 2 3 1``````

When using this expression with the roman digits string we get the equivalent positions:

``````      'IVXLCDM' ⍳ 'XVII'
3 2 1 1``````

As shown above we get the positions in the roman digits string. Returning to our original expression we use the selection expression to get the value of each roman digit in decimal form:

``````      (1 5 10 50 100 500 1000)['IVXLCDM' ⍳ 'XVII']
10 5 1 1``````

As you can see here we almost have the result for the conversion of `XVII`. We just have to sum these numbers up to get 17.

Going back to the example and continuing reading for right to left we find and assignment to the `z` variable.

``z←(1 5 10 50 100 500 1000)['IVXLCDM'⍳a]``

The rest of the expression is going to take care of subtracting the value required for converting numbers like: ‘IX’.

Reading the expression from right to left we find:

``(¯1+2×z≥1↓z,0)×z←(1 5 10 50 100 500 1000)['IVXLCDM'⍳a]``

This expression is a multiplication (`×`). For arrays it applies to operation element wise :

``````      2 3 × 4 5
8 15``````

The interesting part here is the expression being used for the left operand of the multiplication:

``(¯1+2×z≥1↓z,0)``

Again we are going to analyze this expression from right to left.

``z,0``

This expression returns the an array with a zero at the end:

``````     (10 20 30),0
10 20 30 0
z←(1 5 10 50 100 500 1000)['IVXLCDM' ⍳ 'IX']
z,0
1 10 0``````

Then the following operation drops the first element of the array:

``````      1↓z,0
10 0``````

The drop expression returns an array without the first ‘n’ elements:

``````      3 ↓ 20 30 40 30 20 10
30 20 10``````

The following section is very interesting . We use the greater equal expression `≥` to determine which elements of the array are greater or equal than the second array:

``````       10 20 30 ≥ 5 21 30
1 0 1``````

Applying this to our example for ‘IX’:

``````      z←(1 5 10 50 100 500 1000)['IVXLCDM' ⍳ 'IX']
z≥1↓z,0
0 1``````

This is interesting: we get an array that indicates which entries are lower than its successor. In the case of IX we are getting `0 1` saying that the first digit needs to be subtracted from the second. The final part performs this operation.

``````      z←(1 5 10 50 100 500 1000)['IVXLCDM' ⍳ 'IX']
z≥1↓z,0
0 1
¯1+2×z≥1↓z,0
¯1 1
(¯1+2×z≥1↓z,0)×z
¯1 10``````

Now we can finally sum all the numbers on the resulting array and get the decimal number. This is performed with the reduce operation `/` (https://aplwiki.com/wiki/Reduce) with the `+` operator.

``````      +/ 10 20 30
60
10 + 20 + 30
60``````

Applying this operation to our partial expression returns the expected value:

``````      (¯1+2×z≥1↓z,0)×z
¯1 10

+/(¯1+2×z≥1↓z,0)×z
9``````

We can see this process in action by executing all the parts with an interesting number like `MMCXXIX` (2129).

``````      z←(1 5 10 50 100 500 1000)['IVXLCDM'⍳a]
z
1000 1000 100 10 10 1 10
z,0
1000 1000 100 10 10 1 10 0
1↓z,0
1000 100 10 10 1 10 0
z≥1↓z,0
1 1 1 1 1 0 1
¯1+2×z≥1↓z,0
1 1 1 1 1 ¯1 1
+/(¯1+2×z≥1↓z,0)×z
2129``````