3
$\begingroup$

I have the following function:

f[lst_List /; MatchQ[lst, SOME PATTERN HERE]] := 
  ({#[[1]] + #[[2]], #[[1]] - #[[2]]} &) /@ lst;

I want to match lists that are ordered pairs of reals: {{0.1, 0.2}, {0.3, 0.4}}, and so on. I tried the following pattern:

MatchQ[{{0.1, 0.2},{0.3, 0.4}}, _List[_List[_Real, _Real]]]

but this returned False. I know that:

MatchQ[{{0.1, 0.2},{0.3, 0.4}}, {__List}]

returns True, but this pattern is not sufficiently specific, since it also matches {{}}, among others.

What is the correct way to construct the pattern I want?

$\endgroup$
3
  • 1
    $\begingroup$ MatchQ[{{0.1, 0.2}, {0.3, 0.4}}, List[List[_?NumericQ, _?NumericQ] ..]] ? $\endgroup$ Commented May 5, 2014 at 23:05
  • 1
    $\begingroup$ MatchQ[{{0.1, 0.2}, {0.3, 0.4}}, {{Except[_Complex], Except[_Complex]} ..}] $\endgroup$
    – ciao
    Commented May 5, 2014 at 23:06
  • $\begingroup$ Thank you very much. They work. $\endgroup$ Commented May 5, 2014 at 23:17

2 Answers 2

6
$\begingroup$

There is no need to explicitly refer to MatchQ. Mathematica's pattern language is up to expressing what you want as an argument pattern.

f[lst : {{Repeated[Except[_Complex, _?NumberQ], {2}]} ..}] := 
  ({#[[1]] + #[[2]], #[[1]] - #[[2]]}&) /@ lst
f @ {{0.1, 0.2}, {0.3, 0.4}, {1/2, 1}, {2, 2.}}
{{0.3, -0.1}, {0.7, -0.1}, {3/2, -(1/2)}, {4., 0.}}

The function does not evaluate for lists containing pairs having a complex component.

f @ {{1, 1}, {1, I}}
f[{{1, 1}, {1, I}}]
$\endgroup$
4
$\begingroup$

It is not defined in the question what you wish to consider "real" for the purpose of this pattern. Is Pi real for your purposes for example? If you want to match only explicit decimal values you should use _Real, e.g. {{_Real, _Real} ..}. If you want something more general I propose:

realQ = Re[#] == # &;

Because Re is Listable this can be applied to arrays, allowing:

f[lst : {{_, _} ..}?realQ] := (* body *)

This is clear, concise, and has lower overhead than what m_goldberg proposed.

$\endgroup$
1
  • $\begingroup$ Thank you very much for this explanation. $\endgroup$ Commented Jul 9, 2014 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.