PreviousUpNext

10.8.3  Sub-Pattern Matching

Pattern variables maybe used to match entire nested structures, not just individual elementary values:

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

    case ((1,2),(3,4),(5,6))
        (a,b,c) => printf "Pairwise sums: %d, %d, %d\n" ((+)a) ((+)b) ((+)c);
    esac;

    linux$ ./my-script
    Pairwise sums: 3, 7, 11

Here we are taking advantage of the fact that the Mythryl binary addition operator + actuall operates upon pairs of integers; by using the (+) notation to convert it from an infix operator into a normal prefix function, we can apply it directly to the matched pairs of integers.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext