5

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

Let B be a string, which in APL terms can be either a scalor or a vector. If it's a scalar, ⍴B returns null rather than the length of the string as I want.

B←'QR'
⍴B ⍝ returns 2

B←'Q'
⍴B ⍝ returns null

I discovered one way around that:

⍴1↓'X',B ⍝ concatenating X and then removing it returns a value of 1

That works, but it seems a little hokey, so I'm wondering if there is a more standard way to find string length.

Is it just me or does this seem a little inconsistent? The tutorial I read said to think of a scalar as a point similar to the way it is 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 results in a scalar?

I'm really enjoying APL, so this question isn't meant as criticism. My question is, what's the best way to find string length? And, if anyone can shed a little light on this seeming inconsistency, it would be appreciated.

Expedito
  • 7,771
  • 5
  • 30
  • 43

2 Answers2

5

The reasons that concatenating X and removing it works, is that the catenation produces a vector. Removing X with 1↓ then leaves it as a vector, as an empty vector to be precise. And the length of vectors can be measured with ⍴. That is also what CrazyMetal's solution does: monadic , transforms its argument (scalar or array of any dimension) into a vector. And measuring its rho gives you the answer you were looking for: the standard way to find string length.

⍴,B
MBaas
  • 7,248
  • 6
  • 44
  • 61
  • Thanks, but it doesn't address the inconsistency part. You can get a vector from concatenating scalars, but you can't get a scalar by dropping elements. I realize that it remains a vector, but it doesn't make sense to me why. However, I like solution. It's much neater than my solution. Thanks! – Expedito Jun 26 '13 at 08:08
  • Dropping an element means you changing the size ob the object, and that is all. You do not change the rank by changing the size. I see that is must be difficult to grasp initially, but once you get used to "array thinking", it will feel natural ;-) – MBaas Jun 26 '13 at 15:25
  • A related observation: monadic take (sorry no APL chars on ipad keyboard!) returns the first element of a vector as a scalar. I'm afraid I'm a bit far removed from APL these days to recall the reason for apparent inconsistency cited. – RFlack Feb 03 '14 at 22:01
  • Are you using Dyalog? The behaviour of monadic take there depends on quadML, the migration level. And there is a setting (sorry, travelling right now w/o manual and APL) where it would behave like the "first"-function... – MBaas Feb 06 '14 at 15:15
  • Sorry MBaas just saw yr question. My main APL experience was IBM mainframe APL2. – RFlack May 23 '14 at 03:29
  • The "inconsistency" is an aspect of the notation. A lone single character or numeric quantity is interpreted to be scalar. rho 'A' is iota 0, i.e. a scalar. rho 'AB' is 2, a 2 element vector. rho ,'A' is 1, a one element vector The same is true for numbers, rho 42 is a scalar. Also, you can test for rank with rho rho, i.e. rho rho 'A' would be zero, a scalar. – Lobachevsky Sep 04 '14 at 11:17
2

The behaviour that you see is caused by an inconsistency in the APL syntax. A sequence of characters enclosed in single-quotes creates an array of those characters. For example, 'foo' creates an array of the characters f, o and o. Calling on this array shows the expected result:

      ⍴'foo'
3

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

As was suggested by Pé de Leão, you can use the , function to ensure that a scalar value becomes an array which is why using ⍴, works regardless of whether its argument is a scalar or an array.

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

Elias Mårtenson
  • 3,820
  • 23
  • 32