MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Passing arguments and pattern matching

  • To: mathgroup at
  • Subject: [mg33826] RE: [mg33792] Passing arguments and pattern matching
  • From: "David Park" <djmp at>
  • Date: Fri, 19 Apr 2002 02:27:52 -0400 (EDT)
  • Sender: owner-wri-mathgroup at


The first list doesn't contain a list, so the "pattern" does not match. What
you really want is a pattern that consists of all the alternatives in the
second list. So, rewrite your definition this way:

myPattern[lista_List, listb_List] := Position[lista, Alternatives @@ listb]

 a = {x, Log[x], y, x y, Log[y] };
 b = {x};

myPattern[a, b]
{{1}, {2, 1}, {4, 1}}

myPattern[a, {Log[x]}]

b = {x, Log[x]};
myPattern[a, b]
{{1}, {2, 1}, {2}, {4, 1}}

David Park
djmp at

> From: Coleman, Mark [mailto:mark.coleman at]
To: mathgroup at
> 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!

  • Prev by Date: Re: Passing arguments and pattern matching
  • Next by Date: RE: Re: list of digits to string
  • Previous by thread: Re: Passing arguments and pattern matching
  • Next by thread: Making Tensor operations more efficient