Quantcast
Channel: Pattern matching tuples as inputs to pure function - Mathematica Stack Exchange
Viewing all articles
Browse latest Browse all 3

Pattern matching tuples as inputs to pure function

$
0
0

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]= TrueOut[59]= FalseOut[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!


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images