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\n" list; because printf expects a string.
To use printf, the list needs to be turned into a string using strcat().
printf "%s\n" ( strcat( [ "abc", "def" ] ) );Variables can be used in the usual ways:# =># abcdef
list = [ "abc", "def" ]; printf "%s\n" ( strcat( list ) );# =># abcdef
a = "abc"; b = "def"; list = [ a, b ]; printf "%s\n" ( 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 this example, printf is applied individually to each element.
list = [ "test", "list" ]; apply printf "'%s'\n" list;In the above example, each element is encapsulated by single quotes ( ' ) and separated by a newline ( \n ).# =># '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", "def" ]; list = map toupper( list ); apply printf "'%s'\n" list;# =># 'ABC'# 'DEF'
! - Prepending to a list
Prepending a string to a list is done with the exclamation point ( ! ).
list = [ "def", "ghi" ]; list = "abc" ! list; apply printf "'%s'\n" list;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.# =># 'abc'# 'def'# 'ghi'
@ - 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'\n" 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( a ); printf "%s\n" ( string );Note that you don't need (and can't use) strcat() in this case. head() expects a list (of strings), and returns a String.# =># abc
tail()
tail() returns a list of everything after the first element.
list = [ "abc", "def", "ghi" ]; list = tail( list ); apply printf "'%s'\n" list;# =># '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() will drop any duplicate strings.
list = [ "ghi", "abc", "def", "abc", "def", "ghi", "def" ]; list = struniqsort( a ); 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( a ); string = strcat( list ); printf "%s\n" string;* Pseudo-randomized. Perhaps dbehicgfa.# =># *
length()
Where strlen() gives the length of a string, length(), counts the number of elements in an array.
a = [ "abc", "def", "ghi" ]; a = length( a ); printf "%d\n" a;# =># 3
More list functions
- [1] The standard list API
tail : list(X ) -> list(X );tail() accepts a list, and returns a list.