The length of the string with scalars and vectors in APL

I just started learning APL a couple of weeks ago, so this may sound like a beginner's question.

Let B be a string, which in terms of APL can be either a scaler or a vector. If it is a scalar, ā“B returns zero, not the string length, as I want.

 B←'QR' ā“B ā returns 2 B←'Q' ā“B ā returns null 

I discovered one way:

 ā“1↓'X',B ā concatenating X and then removing it returns a value of 1 

This works, but it seems a bit hockey, so I wonder if there is a more standard way to find the length of a string.

Is it just me or does it seem a little inconsistent? The textbook that I read said that it is considered scalar as a point, similar to how it is done in vector algebra. But how is it that concatenating a scalar to a scalar makes a vector, but dropping a scalar from a vector never leads to a scalar?

I really like the APL, so this question does not mean criticism. My question is: what is the best way to find the length of a string? And, if someone can shed some light on this seeming inconsistency, it will be appreciated.

+4
source share
2 answers

The reasons associated with merging X and removing it are because catenation creates a vector. Removing X with 1↓ then leaves it as a vector, and the empty vector is accurate. And the length of the vectors can be measured with ā“. It also means the CrazyMetal: monadic solution , converts its argument (scalar or array of any dimension) to a vector. And measuring it rho gives you the answer you were looking for: The standard way to find the length of a string.

 ā“,B 
+4
source

The behavior you see is caused by inconsistency in the APL syntax. A sequence of characters enclosed in single quotes creates an array of these characters. For example, 'foo' creates an array of characters f , o and o . Calling ā“ in this array shows the expected result:

  ā“'foo' 3 

However, this is an exception to this rule, and this is where the syntax is inconsistent. When you specify one character inside single quotes, you do not create an array of one character, but return the character itself. This is why ā“'f' returns an empty array, since the value is a scalar.

As suggested by PĆ© de LeĆ£o, you can use the , function to ensure that the scalar value becomes an array, so using ā“, works whether its argument is a scalar or an array.

Due to this inconsistency, the GNU APL has an extension that allows you to use " instead of ' when entering strings. The only difference between them is that " always creates an array, even for single characters.

+1
source

Source: https://habr.com/ru/post/1488061/


All Articles