See this example:
(let [x 1 y 2]
(match [x y]
[1 b] b
[a 2] a
:else nil))
;=> 2
I can't get my head around a few things:
- Does
1matchxand gets bound tob? - Does
2matchyand gets bound toa? - Assuming I got the two points above right, why return
ainstead ofbconsidering they both matched part of[x y]. Is it because it is the last clause?
Think of each pattern as a template to be matched to the input
[x y]or[1 2].The first pattern is
[1 b]which matches the input because the first template item is a matching literal value1, and the second template item is a binding that will hold any value in that position of the input, which happens to be2in this case. Thatbbinding is accessible from the righthand side of the match clause, as if it were aletbinding.This example might demonstrate it more clearly:
The pattern is a match, but it doesn't matter because the preceding pattern was already a match. If it were the successful match,
awould be bound to1.