I know this has already been asked for Perl.
Does Raku make the same distinction between arrays and lists?
If your data does not require any arithmetic operations, then a list can be a better choice, as it supports better in-built functions for data manipulation. On the other hand, arrays should be used when mathematical operations need to be performed.
Arrays can directly handle arithmetic operations while lists cannot. So we use arrays over lists. Arrays are preferred over lists for a longer sequence of data items.
Arrays can store data very compactly and are more efficient for storing large amounts of data. Arrays are great for numerical operations; lists cannot directly handle math operations. For example, you can divide each element of an array by the same number with just one line of code.
While lists and arrays are superficially similar—they are both multi-element data structures—they behave quite differently in a number of circumstances. First of all, lists are part of the core Python programming language; arrays are a part of the numerical computing package NumPy.
TL;DR It makes a distinction, but not exactly the same one. In fact, it's very different in detail.
There's the general informal concept of a "list" (with a lowercase 'l') which is the notion of one thing, then another, then another.
There are variants of this general notion: lists of arguments (captures), lists of parameters (signatures), lists of values, etc.
At this informal level the words "list" and "array" are largely interchangeable though "list" tends to emphasize immutability, "array" mutability.
Positional
, Iterable
, List
, Array
, Seq
, etc.More formally, listy things are typically types that "do" listy roles. The two main listy roles are the Positional
and Iterable
.
There is a List
(capital 'L') type which does
the Positional
and Iterable
roles. There is also an Array
type which is a sub-class of List
. And there are native arrays which also do the Positional
and Iterable
roles but are array
s, not Array
s nor List
s:
my int @array;
say @array ~~ Array; # False
say @array ~~ array; # True
The behavioral differences between List
s and arrays (either Array
s or array
s) relate to mutability. A List
can easily be constructed so that it is guaranteed immutable. In contrast an array can never be intrinsically guaranteed immutable (though of course code can be written to not mutate it after construction). More specifically:
Adding or subtracting elements. A List
's length cannot be changed after its construction. Methods such as .push
will fail on a List
. In contrast an array only has a fixed length if it's declared as having a fixed length at construction time.
Assigning and/or binding to elements. A List
element is never rebindable, and is only assignable if it's bound to an lvalue at construction time. In contrast an Array
element is always rebindable (I'm ignoring the NYI ::=
), and is only not assignable when it's bound to an immutable value.
There are several other important listy types, for example:
A Seq
is "An iterable, lazy sequence of values".
A Capture
is used for various purposes, most notably for containing argument lists and as the parent type of the regex/grammar Match
class.
The "official" docs at doc.raku.org has a Lists, Sequences, and Arrays page that should be helpful.
Let me just add that in Perl, a list an ephemeral thing; in Raku it's a regular type, and a comma-separated list of literals produces one:
$ raku -e 'say (1, 2, 3).^name'
List
Arrays also support the push
, pop
, shift
, unshift
and splice
methods that Lists do not have.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With