Re: Passing arguments and pattern matching

*To*: mathgroup at smc.vnet.net*Subject*: [mg33828] Re: [mg33792] Passing arguments and pattern matching*From*: BobHanlon at aol.com*Date*: Fri, 19 Apr 2002 02:28:02 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

In a message dated 4/16/02 7:37:53 AM, mark.coleman at dri-wefa.com writes: >I have a question on pattern matching that I have been struggling with. >First I'll describe the issue broadly, and then attempt to illustrate it >with a specific example. My intution is that is a relatively simple answer, >but I have been unable to find it. > >I would like to write a general function that accepts these two lists as >arguments, i.e., myPattern[a_List,b_List]. The function would act something >like the built-in Mathematica Postion[] pattern function and return the >positions of any elements of b that occur in List a. This function will >be called by other functions, so the user will not know in advance the >values of lists a or b. In my attempts to build this function I have used >the Position[] function. The difficulty is that Position treats the arguments >I pass it as 'literal', and not as references to what the arguement represents. > >For instance, let's say I define two lists as follows: > > a = {x, Log[x], y, x y, Log[y] }; > b = {x}; > >Now say I wish to compute the position of x or various functions of x in >a, then of course > > >In[24]:= Position[a,x] > >Out[24]= {{1},{2,1},{4,1}} > >In[26]:= Position[a,Log[x]] > >Out[26]= {{2}} > >and so on. But now assume I wish to define a function > > myPattern[lista_List,listb_List] := Position[lista,listsb] > >If I call myPatten[a,b], this is the same as executing > > >In[27]:=Position[a,b] > >Out[27]= {} > >My question is, how do I have Mathematica treat the second argument not >as a literal ("b"), but as representing a the value of the underlying argument? >For instance > > myPattern[a,{Log[x]}] would return {{2}} > This may be what you want. myPattern[lista_List,listb_List]:=Position[lista,#]& /@ listb; a={x,Log[x],y,x y,Log[y]}; b={x}; myPattern[a,b] {{{1}, {2, 1}, {4, 1}}} myPattern[a,a] {{{1}, {2, 1}, {4, 1}}, {{2}}, {{3}, {4, 2}, {5, 1}}, {{4}}, {{5}}} Bob Hanlon Chantilly, VA USA