MathGroup Archive 2002

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

Search the Archive

Passing arguments and pattern matching

  • To: mathgroup at
  • Subject: [mg33792] Passing arguments and pattern matching
  • From: "Coleman, Mark" <mark.coleman at>
  • Date: Tue, 16 Apr 2002 03:50:54 -0400 (EDT)
  • Sender: owner-wri-mathgroup at


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


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}}


  • Prev by Date: acsplines smoothing
  • Next by Date: Re: Subscript[x, y] and the Symbol x
  • Previous by thread: acsplines smoothing
  • Next by thread: Re: Passing arguments and pattern matching