MathGroup Archive 2002

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

Search the Archive

Re: Passing arguments and pattern matching

  • To: mathgroup at smc.vnet.net
  • Subject: [mg33836] Re: [mg33792] Passing arguments and pattern matching
  • From: "Martin Jenkins" <lamarth at optushome.com.au>
  • Date: Fri, 19 Apr 2002 02:28:25 -0400 (EDT)
  • References: <200204160750.DAA20097@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Mark,

myPattern[lista_, listb_] := Flatten[Map[Position[lista, #] &, listb], 1]

appears to do what you have requested.  The Flatten is so that it comes out
with the level of nesting in your desired output.

In[10]:= myPattern[a, {x, Log[x]}]
Out[10]= {{1}, {2, 1}, {4, 1}, {2}}

I'm sure you can apply a Sort of some kind if you wish it.

I would recommend not requiring the inputs to be of type "List" unless it
does not make sense to have something else as the head (in my own project I
have found that most built-in Mathematica functions are like this, and have
cursed the ones that are not).  Certainly, Position works with something as
a head other than List.

Martin Jenkins

----- Original Message -----
From: "Coleman, Mark" <mark.coleman at dri-wefa.com>
To: mathgroup at smc.vnet.net
Subject: [mg33836] [mg33792] Passing arguments and pattern matching


> 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: Bug in NSolve?
  • Next by Date: RE: More accuracy in Disk
  • Previous by thread: Passing arguments and pattern matching
  • Next by thread: Re: Passing arguments and pattern matching