About ∞
Lists are somewhat like an array or a hash from other programming languages. However, lists in Mythryl are items of all the same type. A Mythryl list would be all String, all Int, etc.
Defining a list ∞
This is a string:
string = "abc";
This is a list of strings:
list = [ "abc", "def" ];
strcat() – Displaying a list ∞
Taking our list of strings:
list = [ "abc", "def" ];
You cannot output that list with printf "%s" list; because printf expects a string.
To use printf, the list needs to be turned into a string using strcat().
printf "%s" ( strcat( [ "abc", "def" ] ) ); # => # abcdef
Variables can be used in the usual ways:
list = [ "abc", "def" ]; printf "%s" ( strcat( list ) ); # => # abcdef
a = "abc"; b = "def"; list = [ a, b ]; printf "%s" ( strcat( list ) ); # => # abcdef
apply – Displaying a list (properly) ∞
While strcat() can be used to display a list, apply can display a list more neatly.
apply iterates through a list, applying a function to each element. In the following example, printf is applied individually to each element.
list = [ "test\n", "list" ]; apply printf "'%s'" list; # => # 'test' # 'list'
map ∞
map applies a function to each element of a list.
Unlike apply, map builds a return list. This lets us assign the return list to a named value.
list = [ "abc\n", "def" ]; list = map toupper( list ); apply printf "%s" list; # => # ABC # DEF
! – Prepending to a list ∞
Prepending a string to a list is done with the exclamation point ( ! ).
list = [ "def\n", "ghi" ]; list = "abc" ! list; apply printf "%s" list; # => # abc # def # ghi
A list must have elements that are all the same type, so you cannot prepend a different type. For example, you cannot prepend an Int to a list of Strings.
@ – Combining lists ∞
Where + (plus) combines strings together and ! (exclamation point) prepends a string to a list, @ (at) combines lists together.
a = [ "one", "two" ]; b = [ "three", "four" ]; list = a @ b; apply printf "%s" list; # => # one # two # three # four
Built-in functions, on lists ∞
Various other functions will work on lists.
reverse() ∞
Reverses each element. Note that this does not reverse any of the contents within the elements.
list = [ "abc", "def", "ghi" ]; list = reverse( list ); apply printf "%s\n" list; # => # ghi # def # abc
head() ∞
head() returns the first element of a list.
list = [ "abc", "def", "ghi" ]; string = head( list ); printf "%s" ( string ); # => # abc
Note that you don’t need (and can’t use) strcat() in this case. head() expects a list (of strings), and returns a String.
tail() ∞
tail() returns a list of everything after the first element.
list = [ "abc", "def", "ghi" ]; list = tail( list ); apply printf "%s\n" list; # => # def # ghi
strsort() ∞
strsort() sorts a list of strings.
list = [ "ghi", "abc", "def" ]; list = strsort( list ); apply printf "%s\n" list; # => # abc # def # ghi
struniqsort() ∞
struniqsort() is similar to strsort() and will also drop any duplicate strings.
list = [ "ghi", "abc", "def", "abc", "def", "ghi", "def" ]; list = struniqsort( list ); apply printf "%s\n" list; # => # abc # def # ghi
shuffle() ∞
Makes a list pseudo-randomized.
list = [ "a", "b", "c", "d", "e", "f", "g", "h", "i" ]; list = shuffle( list ); string = strcat( list ); printf "%s\n" string; # => # * }} {{!!*!}} Pseudo-randomized. Perhaps `dbehicgfa`. Randomization is a complex topic which I hope to cover in greater detail later. == `length()` Where `strlen()` gives the length of a string, `length()`, counts the number of elements in an array. {{{ lang="c" list = [ "abc", "def", "ghi" ]; list_length = length( list ); printf "%d\n" list_length; # => # 3
More list functions ∞
From the documentation, we can see the description for tail()
tail : list(X ) -> list(X );
tail() accepts a list, and returns a list.
