PreviousUpNext

5.2.9  Lists and Strings

Mythryl strings are a bit like C strings, but a lot more like Perl or Python strings.

Mythryl strings are a first-class type with a variety of predefined operations:

    linux$ my

    eval:  "abc" + "def";

    "abcdef"

    eval:  toupper("abc");

    "ABC"

    eval:  tolower("ABC");

    "abc"

    eval:  ^D
    linux$

Mythryl lists are a teeny bit like those of Perl and a lot more like those of Python, Lisp or Ruby. Lists are one of the conveniences which make programming in Mythryl much more pleasant that programming in C for many common sorts of tasks. Lists are one of the Mythryl programmer’s favorite datastructures. Any time you have an indeterminate number of similar things which you need to keep track of, you usually just throw them in a list.

Mythryl lists are written with square brackets. You need to put blanks around them to keep the Mythryl compiler from confusing them with arrays.

Useful operations on lists include @ which concatenates two lists to form a single list, strcat which concatenates a list of strings to produce a single string, and reverse which reverses a list:

    #!/usr/bin/mythryl

    a = ["abc", "def", "ghi"];
    b = ["jkl", "mno", "pqr"];

    printf "strcat a     == '%s'\n"  (strcat(a));
    printf "strcat b     == '%s'\n"  (strcat(b));
    printf "strcat a@b   == '%s'\n"  (strcat(a @ b ));
    printf "strcat( reverse(a) ) == '%s'\n"  (strcat(reverse(a)));
    printf "strcat( reverse(a@b) ) == '%s'\n"  (strcat(reverse(a@b)));

When run the above produces:

    linux$ ./my-script
    strcat a     == 'abcdefghi'
    strcat b     == 'jklmnopqr'
    strcat a@b   == 'abcdefghijklmnopqr'
    strcat( reverse(a) ) == 'ghidefabc'
    strcat( reverse(a@b) ) == 'pqrmnojklghidefabc'
    linux$

The map function applies a given function to all elements of a list and returns a list of the results. It is one of the most frequently used Mythryl functions:

    linux$ my

    eval:  map toupper ["abc", "def", "ghi"];

    ["ABC", "DEF", "GHI"]

    eval:  ^D
    linux$

The apply function is just like the map function except that it constructs no return list — the supplied function is applied only for side-effects:

    #!/usr/bin/mythryl

    apply print ["abc\n", "def\n", "ghi\n"];

When run the above produces:

    linux$ ./my-script
    abc
    def
    ghi
    linux$

The Mythryl head and tail functions return the first element of a list and the rest of the list. They correspond to Lisp car and cdr. The Mythryl infix operator ’!’ prepends a new element to a list, returning a new list. It corresponds to the Lisp cons function:

    linux$ my

    eval:  a = [ "abc", "def", "ghi" ];

    ["abc", "def", "ghi"]

    eval:  head(a);

    "abc"

    eval:  tail(a);

    ["def", "ghi"]

    eval:  "abc" ! ["def", "ghi"];

    ["abc", "def", "ghi"]

    eval:  ^D
    linux$ 

The Mythryl explode and implode functions convert between strings and lists of characters:

    linux$ my

    eval:  explode("abcdef");

    ['a', 'b', 'c', 'd', 'e', 'f']

    eval:  implode( ['a', 'b', 'c', 'd', 'e', 'f'] );

    "abcdef"

    eval:  implode( reverse( explode( "abcdef" ) ) );

    "fedcba"

    eval:  ^D

    linux$

The strsort function sorts a list of strings, and struniqsort does the same while dropping duplicates. The shuffle function re-arranges the list elements into a pseudo-random order:

    linux$ my

    eval:  strsort( ["def", "abc", "ghi", "def", "abc", "ghi"] );

    ["abc", "abc", "def", "def", "ghi", "ghi"]

    eval:  struniqsort( ["def", "abc", "ghi", "def", "abc", "ghi"] );

    ["abc", "def", "ghi"]

    eval:  shuffle( ["abc", "def", "ghi", "jkl", "mno"] );

    ["ghi", "def", "abc", "jkl", "mno"]

    eval:  ^D
    linux$

The length function counts the number of elements in a list; the strlen function counts the number of characters in a string:

    linux$ my

    eval:  length( ["abc", "def", "ghi"] );

    3

    eval:  strlen( "abcdefghi" );

    9

    eval:  ^D

    linux$

Comments and suggestions to: bugs@mythryl.org

PreviousUpNext