tuple.icn: Procedure to process n-tuples

link tuple
June 10, 1988; William H. Mitchell
This file is in the public domain.

   This procedure implements a "tuple" feature that produces the effect
of multiple keys.  A tuple is created by an expression of the
form

     tuple([exrp1, expr2, ..., exprn])

The result can be used in a case expression or as a table subscript.
Lookup is successful provided the values of expr1, expr2, ..., exprn
are the same (even if the lists containing them are not).  For example,
consider selecting an operation based on the types of two operands.  The
expression

     case [type(op1), type(op2)] of  {
        ["integer", "integer"]:  op1 + op2
        ["string", "integer"] :  op1 || "+" || op2
        ["integer", "string"] :  op1 || "+" || op2
        ["string", "string"]  :  op1 || "+" || op2
        }

does not work, because the comparison in the case clauses compares lists
values, which cannot be the same as control expression, because the lists
are different, even though their contents are the same.  With tuples,
however, the comparison succeeds, as in

     case tuple([type(op1), type(op2)]) of {
        tuple(["integer", "integer"]):  op1 + op2
        tuple(["string", "integer"]) :  op1 || "+" || op2
        tuple(["integer", "string"]) :  op1 || "+" || op2
        tuple(["string", "string"])  :  op1 || "+" || op2
        }

Source code | Program Library Page | Icon Home Page