10.8.4  As Patterns

Sometimes we need to use pattern matching to assign a name to both a complete subpattern and also its individual constituents.

The as pattern operator satisfies this need:

    linux$ cat my-script

    case ((1,2),(3,4),(5,6))
        ( pair1 as (a,b),
          pair2 as (c,d),
          pair3 as (e,f)
        => printf "(%d,%d) sum to %d, (%d,%d) sum to %d, (%d,%d) sum to %d\n"
               a b ((+)pair1)
               c d ((+)pair2)
               e f ((+)pair3);

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

Comments and suggestions to: