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

Pathological Answer

This is the answer to the following question:

Pathological Question

What is the bound value of pattern variable $y after matching the following two tuple patterns:

Tuple pattern A:
((ho, $_, ($a, $a)), ($a, $a, $b), ($a, *$b))
Tuple pattern B:
($x, $x, $y)

Answer

Let's take this step by step, matching each element of the two tuple patterns in turn.

  1. Match (ho, $_, ($a, $a)) to $x.

    This succeeds with the following binding:

    $x:

    (ho, $_, ($a, $a))

  2. Match ($a, $a, $b) to $x.

    Because $x is bound to a value, this becomes the same as matching:

    • ($a, $a, $b) to
    • (ho, $_, ($a, $a))

    Which succeeds, binding:

    $a:

    ho

    $b:

    ($a, $a)

    $_ is an anonymous variable, so it is not bound (or bound to).

  3. Match ($a, *$b) to $y.

    Because both $a and $b have bound values, this becomes the same as matching:

    • (ho, ho, ho) to
    • $y

    Which succeeds, binding:

    $y:

    (ho, ho, ho)

So the overall match succeeds with the following bindings:

$x:
(ho, $_, ($a, $a))
$a:
ho
$b:
($a, $a)
$y:
(ho, ho, ho)

And so $y is (ho, ho, ho)!

Note

If you got this right, you should really be using Pyke!

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.