Why would this work:
Clear[f]
f[a : PatternSequence[b_, c_]] := {a};
f[1, 2]
(* {1, 2} *)
and this also works:
Clear[f]
f[a : PatternSequence[_, _] ..] := {a};
f[1, 2, 3, 4]
(* {1, 2, 3, 4} *)
but this does not work?
Clear[f]
f[a : PatternSequence[b_, c_] ..] := {a};
f[1, 2, 3, 4]
(* f[1, 2, 3, 4] *)
Edit: Now that @RunnyKine's answer and @kguler's comment have perfectly answered my original question, I have another related question: is there a pattern-based way that I could extract the first element of the repeated pattern sequence without doing this?
Clear[fNew]
fNew[a : PatternSequence[_, _] ..] := Partition[{a}, 2][[All, 1]]
fNew[1, 2, 3, 4]
(* {1, 3} *)
Answer
This has nothing to do with PatternSequence
rather the problem is with how you use Repeated
(..
). Take for example the following function definition:
f[x : {{_, _} ..}] := Norm[N[x]]
Now if we feed it the following input:
f[{{1, 1}, {1, 2}, {1, 3}}]
The function works as expected and yields:
4.07914333
Now let's redefine the function as follows (we use g
instead)
g[x : {{a_, b_} ..}] := Norm[N[x]]
Now notice it looks just like f
above but we've introduced the pattern objects a_
and b_
We feed it the same input as above:
g[{{1, 1}, {1, 2}, {1, 3}}]
And we get:
g[{{1, 1}, {1, 2}, {1, 3}}]
Well, strange, nothing happens. No match. Now let's try a different input, one where the first pair is repeated:
g[{{1, 1}, {1, 1}, {1, 1}}]
Now we get:
2.44948974
A different input with the first pair repeated:
g[{{1, 2}, {1, 2}, {1, 2}}]
Yields
3.87298335
So, you see that Repeated
works in mysterious ways, well, not really. The point here is that, with no explicit pattern, you get a structural match (for lack of a better term) but with explicit pattern names you have to repeat terms just like the pattern describes. So for your last example, if you do:
f[1, 2, 1, 2]
You get:
{1, 2, 1, 2}
and
f[1, 2, 1, 2, 1, 2]
Gives:
{1, 2, 1, 2, 1, 2}
As expected.
Comments
Post a Comment