bug-apl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-apl] scalar/array


From: Elias Mårtenson
Subject: Re: [Bug-apl] scalar/array
Date: Sat, 4 Mar 2017 18:36:27 +0800

On 4 March 2017 at 05:57, <address@hidden> wrote:

what is the reason? do you know their reasoning

cause   1  is 1 element   and 1 2 3 is 3 elements   so   ⍴ 1 2 3   is 3    and  ⍴ 1  should have been 1

But that's wrong. 1 is not a one-element array. 1 is just a number. 1 2 is a two-element array. Each element being a number.

The fact that 1 is interpreted as a number, as opposed to a single-element array is just an artifact of the language. One could easily modify the APL parser to yield such result, but it wouldn't be very useful in most cases.

I guess that by now you're wondering why even have the concept of a scalar? Why not make numbers being a one-dimensional array with size 1? Well, for some operations, behaviour would remain the same, but there are situations you definitely want to be able to tell "a single number" (i.e. a scalar) vs. "an array". For example, A=B behaves differently depending on whether A is a single-element array or a scalar value.

Note that this beahviour isn't limited to APL. Lisp also has a concept of zero-dimensional arrays (although it doesn't take it as far as APL in that numbers and zero-dimensional arrays still have different types). From the Hyperspec:

If the rank of an array is zero then the array is said to have no dimensions, and the product of the dimensions (see array-total-size) is then 1; a zero-rank array therefore has a single element.

Finally, the idea of scalars being zero-dimensional arrays makes sense from a maths standpoint as well:

https://math.stackexchange.com/questions/664594/why-mathbf0-vector-has-dimension-zero
https://en.wikipedia.org/wiki/Dimension_(vector_space)

Regards,
Elias

reply via email to

[Prev in Thread] Current Thread [Next in Thread]