Re: Passing arguments and pattern matching
- To: mathgroup at smc.vnet.net
- Subject: [mg33821] Re: [mg33792] Passing arguments and pattern matching
- From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
- Date: Fri, 19 Apr 2002 02:27:41 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
The answer to your question seems indeed rather simple but it may be that I have just missed yur point . Anyway it seems to me that the following does what you are asking for(?): In[1]:= a = {x, Log[x], y, x y, Log[y] }; In[2]:= b = {x}; In[3]:= myPattern[lista_List,listb_List] := Position[lista,#]&/@listb In[4]:= myPattern[a,b] Out[4]= {{{1},{2,1},{4,1}}} In[5]:= myPattern[a,{Log[x]}] Out[5]= {{{2}}} Andrzej Kozlowski Toyama International University JAPAN http://platon.c.u-tokyo.ac.jp/andrzej/ On Tuesday, April 16, 2002, at 04:50 PM, Coleman, Mark wrote: > Greetings, > > 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}} > > Thanks! > > > >