On 4 March 2017 at 05:57, <enz...@gmx.com> 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 <http://clhs.lisp.se/Body/15_aac.htm>:

*If the rank of an array <http://clhs.lisp.se/Body/26_glo_a.htm#array> is
zero then the array <http://clhs.lisp.se/Body/26_glo_a.htm#array> is said
to have no dimensions <http://clhs.lisp.se/Body/26_glo_d.htm#dimension>,
and the product of the dimensions (see array-total-size
<http://clhs.lisp.se/Body/f_ar_tot.htm#array-total-size>) is then 1; a
zero-rank array <http://clhs.lisp.se/Body/26_glo_a.htm#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