5

I'm an experienced software engineer beginning to study APL, and in order to get a better feel for the language early on, I would like to understand why the language exposes to the coder the difference between scalars and vectors. As far as I can tell this far into my studies, this only limits flexibility without yielding any benefit to compensate for it. The result is APL code cluttered with otherwise-needless workarounds such as ravels, encloses, and discloses.

Since APL is such a bizarre and hard-to-read language, I'm building my own helper library in APL to make my own APL-ish interface, and getting away from using wonky "idioms" in raw APL to do everyday tasks. Before I build in abstraction of the scalar/vector distinction throughout my helper library, should I be aware of any utility I may be sacrificing by doing so?

Thanks!

  • 2
    The only universal place where scalars act differently than any other rank array (besides in built-ins, which can require whatever they want) is in `scalar + array` (with any scalar function not just `+`), where the scalar is repeated for every item of the array. What do you mean by "abstracting away from it"? – dzaima Jan 20 '19 at 16:36
  • 2
    matrices are 2d, vectors 1d, scalars 0d - why cut off at 1? what would the strand `(0)(1)` evaluate to - a simple vector like `0 1` or a nested vector like `(,0)(,1)`? – ngn Jan 20 '19 at 16:58
  • I'm just not seeing the practical benefit to the distinction between a scalar and a vector. When I'm writing functions whose arguments can be vectors, I find I'm always taking the arguments and converting them into nested vectors to avoid unexpected behavior in the case of a single scalar value being passed in. I'm abstracting away the distinction between the two, because I see no benefit in maintaining the distinction. So yes, I always prefer to work with nested vectors like (,value1)(,value2) or even just (,value1) because it makes my code cleaner. Am I misunderstanding something? – Erik Midtskogen Jan 20 '19 at 17:51
  • And as to consistency, I find it odd that the shape of 1 2 3 is 3, the shape of 1 2 is 2 and the shape of 1 is null. – Erik Midtskogen Jan 20 '19 at 17:59
  • 1
    The shape of 1 2 3 being 3 and the shape of 1 being iota zero is a syntactic anomaly of APL. Consider the difference of "c" and 'c' in some of the verbose languages like C# - here the syntax exists to unambiguously differentiate a scalar 'c' with a one-element vector "c". Could have been implemented in APL, but wasn't. For numbers, the best you can do is ,1. – Lobachevsky Jan 30 '19 at 13:38
  • 1
    In other languages, code golf is typically frowned upon. In APL it's the norm. – August Karlstrom Mar 30 '19 at 14:56
  • "Code Golf"!! I've never heard that expression, but it's very clever. And actually, among the APL'ers I know, code golf isn't just "the norm", it's actually an explicit goal. I assume this is because back when they started coding, RAM cost $10 a byte, and this was at a time when $1 bought you a decent workday lunch. – Erik Midtskogen Apr 06 '19 at 01:11

1 Answers1

3

If you're writing a function that requires a vector, I'd suggest you to forget about the possibility of the input being a scalar (consider it undefined behavior), and force the caller to ravel the argument to a vector. It should by far be the minority case (and if it isn't, you're doing something wrong). Just like if you wrote a function taking a matrix, you wouldn't expect it to be given a vector. Using a scalar in a vectors place is as strange as using a vector in a matrices place.

It is indeed strange that 1 2 3 and 1 2 are vectors, but 1 alone is a scalar, so if you want consistency, don't create vectors with A B C notation (aka strand notation) and forget that arrays can be created like that completely. Of course, I don't actually expect you to do that, and I would like if there was a notation that could create any size vectors all in the same way.

Also, don't forget that APL supports arrays with rank > 1, using which correctly are a major part of writing APL well. Scalars are rank 0 (0-dimensional), vectors - 1 (1D), matrices - 2 (2D), etc.

dzaima
  • 388
  • 3
  • 9
  • 2
    Maybe mention what to use instead of strand notation? – Adám Jan 20 '19 at 18:41
  • Cool. Thanks for the guidance. In the APL world, there seems to be plenty of documentation on how the language works, but little in the area of best practices. Coming from a Java/C# world where practicality and safety (i.e. preventing you from making mistakes) is favored over adherence to a theoretical framework (however elegant that framework may look on paper) I'm looking for ways of smoothing out the rough edges and reducing the chances of bugs that only crop up long after the code goes live. So thanks! – Erik Midtskogen Jan 20 '19 at 22:01