Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array vs. list data type?

Tags:

raku

I know this has already been asked for Perl.
Does Raku make the same distinction between arrays and lists?

like image 822
Stefanus Avatar asked Mar 30 '18 19:03

Stefanus


People also ask

Is a list better than an array?

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.

Why use an array instead of a list?

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.

Which is better list or array in python?

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.

Is a list an array?

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.


2 Answers

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.

like image 115
raiph Avatar answered Oct 10 '22 20:10

raiph


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.

like image 27
moritz Avatar answered Oct 10 '22 20:10

moritz