Please Make a Donation:
Support This Project

Hosted by:
Get Python Knowledge Engine (PyKE) at SourceForge.net. Fast, secure and Free Open Source software downloads

Tuple Patterns

Tuple patterns only match tuples. They are written as simply a comma separated list of patterns within parenthesis:

(1, $x, "buckle my shoe")

You can also write a rest pattern variable at the end using an asterisk (*):

($a, fie, $b, *$c)

This will match the rest of the items in the data value that the tuple pattern is matched to. Note that the rest pattern variable is always bound to a tuple.

Examples:

  • matching (1, $x, "buckle my shoe") to (1, 2, "buckle my shoe") matches, binding $x to 2.
  • matching (1, $x, "buckle my shoe") to (1, 2, "buckle my belt") does not match because the third pattern within the tuple pattern fails to match the third value in the matched tuple.
  • matching ($a, fie, $b, *$c) to (fee, fie, foe, fum) matches, binding $a to fee, $b to foe and $c to (fum).
  • matching ($a, fie, $b, *$c) to (fee, fie, foe) matches, binding $a to fee, $b to foe and $c to ().
  • matching ($a, fie, $b, *$c) to (fee, fie) does not match because the data value has to have a length of at least three.

Hint

You can use (*$foo) to only match a tuple. It will bind $foo to the entire tuple, but will fail to match any other data type.

More:

Literal Patterns

Explanation of literal patterns.

Pattern Variables

Explanation of pattern variables.

Tuple Patterns

Explanation of tuple patterns.

Matching Two Patterns

Explanation of matching two patterns together, vs matching a pattern to data.

Page last modified Mon, Oct 27 2008.