PreviousUp

10.8.7  Pattern-Match Statement

Mythryl uses pattern matching in many contexts other than case statements. The simplest is the pattern-match statement, which takes the form:

    my pattern = expression;

This allows efficient unpacking of a nested datastructure into named components:

    linux$ cat my-script
    #!/usr/bin/mythryl

    r = ( (1,2), (3,4), (5,6) );

    my ((a,b), (c,d), (e,f)) = r;

    printf "((%d,%d), (%d,%d), (%d,%d))\n" a b c d e f;

    linux$ ./my-script
    ((1,2), (3,4), (5,6))

In the common case in which the pattern consists of a single variable, the my keyword may be dropped:

    linux$ cat my-script
    #!/usr/bin/mythryl

    i = 12 * 13;

    printf "Product = %d.\n" i;

    linux$ ./my-script
    Product = 156.

This looks superficially much like a C assignment statement; it differs in that the Mythryl pattern-match statement never has any side-effects upon the heap; all it does is create new local names for existing values.


Comments and suggestions to: bugs@mythryl.org

PreviousUp