# Pattern matching tuples as inputs to pure function

I've been having an issue with functions which are supposed to receive as input a list of tuples. My troubles are two-fold:

1. The function doesn't properly identify the pattern test I laid out in the definition of the inputs.
2. The pattern matching itself is not ideal for a list of (same length) tuples and I couldn't find a more suitable pattern to match for.

My issue was originally in a very convoluted function, but I managed to replicate it in a simpler form.

g[x_, {a_, b_}] := a x^b (*generic function - actual definition of G is irrelevant other than the input structure*)
SetAttributes[myfun, HoldFirst]
myfun[f_[args__, pars:{__}], x_ : {{__} ..}] := f[Sequence @@ #, pars] & /@ x


First I check to see if my pattern matches what I want it to do (more or less as it still doesn't properly identify tuples of different lengths).

In:= MatchQ[Tuples[Range@10,1],{{__}..}]
MatchQ[Range@10,{{__}..}]
MatchQ[x,{{__}..}]

Out= True

Out= False

Out= False


Note that a simple Range doesn't trigger the pattern match and neither does a generic symbol x (The symbol returns a weird output which I just noticed after posting this question - the function is not applied and yet the symbol is returned). Despite this:

In:= myfun[g[x, {a, b}], Range[2, 4]] (*Regular range*)
myfun[g[x, {a, b}],Tuples[ Range[2, 4],1]] (*Properly formatted tuples*)
myfun[g[x,{a,b}],x] (*Symbol (!) - still works*)

Out= {2^b a,3^b a,4^b a}

Out= {2^b a,3^b a,4^b a}

Out= x


My only conclusion is that for some reason the function does not evaluate the condition I set. Were I a betting man, my money is likely on problematic syntax. Also, if there is a better way to match a list of tuples I would appreciate the insight.

Help is greatly appreciated!

• If you want to match a list of lists that are all the same length, why not just use x_List?MatrixQ? Dec 29, 2020 at 12:49
• Thank you for your reply, My understanding of pattern matching syntax in pure function input is a little lacking and not well formed. Is there a good reference for it? I haven't found much by way of the documentation.
– Ben
Dec 29, 2020 at 14:48
• I'd recommend starting here. The ? syntax (PatternTest) is explained a little further down. Dec 29, 2020 at 18:29
• Your suggestion was very helpful! Thank you for the reference as well, I will make sure to read further into this subject.
– Ben
Dec 30, 2020 at 0:26

I figured out one of my issues on my own and figured I would update this question in case others happen upon it.

I had a syntactic error in the definition of my function.

myfun[f_[args__, pars:{__}], x_ : {{__} ..}]

myfun[f_[args__, pars:{__}], x : {{__} ..}]

• So does x : {{_, _}..} do what you want better? May 23 at 16:09