Array vs. list data type?

TL;DR It makes a distinction, but not exactly the same one. In fact, it's very different in detail.

Informal "list" and "array" concepts

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.

Formal types: 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 arrays, not Arrays nor Lists:

my int @array;
say @array ~~ Array; # False
say @array ~~ array; # True

The behavioral differences between Lists and arrays (either Arrays or arrays) 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.

Tags:

Raku