PreviousUpNext

5.2.12  Mythryl foreach Loops

The C-style for loop is not used very heavily in Mythryl, but another form of loop construct, the foreach, comes in very handy. The foreach loop is not actually a compiler construct at all, just a library routine. The foreach loop iterates over the members of a list:

    #!/usr/bin/mythryl

    foreach ["abc", "def", "ghi"] {.
        printf "%s\n" #i;
    };

Note the dot before the curly brace and the sharp before the i loop variable. This syntax looks a little odd at first blush. It will make more sense once we have discussed Mythryl thunk syntax.

When run, the above does just what you probably expect:

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

The foreach loop is more common than the for loop in Mythryl code primarily because lists are more common than arrays. Mythryl has a profusion of library routines which construct or transform lists. For example the .. operator constructs a list containing a sequence of integers:

    linux$ my

    eval:  1 .. 10;

    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    eval:  foreach (1..10) {. printf "%d\n" #i; };
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    ()

    eval:  ^D

    linux$

Comments and suggestions to: bugs@mythryl.org

PreviousUpNext