0
$\begingroup$

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[58]:= MatchQ[Tuples[Range@10,1],{{__}..}]
MatchQ[Range@10,{{__}..}]
MatchQ[x,{{__}..}]

Out[58]= True

Out[59]= False

Out[60]= 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[68]:= 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[68]= {2^b a,3^b a,4^b a}

Out[69]= {2^b a,3^b a,4^b a}

Out[70]= 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!

$\endgroup$
4
  • 2
    $\begingroup$ If you want to match a list of lists that are all the same length, why not just use x_List?MatrixQ? $\endgroup$ – Sjoerd Smit Dec 29 '20 at 12:49
  • $\begingroup$ 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. $\endgroup$ – Ben Dec 29 '20 at 14:48
  • $\begingroup$ I'd recommend starting here. The ? syntax (PatternTest) is explained a little further down. $\endgroup$ – Sjoerd Smit Dec 29 '20 at 18:29
  • $\begingroup$ Your suggestion was very helpful! Thank you for the reference as well, I will make sure to read further into this subject. $\endgroup$ – Ben Dec 30 '20 at 0:26
0
$\begingroup$

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.

Instead of

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

I should have written

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

Note the underscore near the x input. I still don't have a better way to identify tuples.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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