MathGroup Archive 2000

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

Search the Archive

Re: List of patterns for which a function is defined?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg24406] Re: [mg24378] List of patterns for which a function is defined?
  • From: "Johannes Ludsteck" <ludsteck at zew.de>
  • Date: Tue, 18 Jul 2000 00:58:06 -0400 (EDT)
  • Organization: Zentr. f. Europ. Wirtschaftsforsch
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,
DownValues[] together with Map gives you the desired information:

In[1]:=	a[1] = 4; a[2] = 5; a["cat"] = "sam";

In[2]:=		dv = DownValues[a]
Out[2]=
			{HoldPattern[a[1]] :> 4, HoldPattern[a[2]] :> 5, 
  			HoldPattern[a["cat"]] :> "sam"}

Now you are in the position to access the keys:

In[3]:=		Map[#[[1, 1]] &, dv]
Out[3]=	{4, 5, "sam"}

The function which does the whole job is a one-liner:
In[4]:= 		ListOfPatternsDefined[p_] :=
				Map[#[[1, 1]] &, DownValues[p]]

Best regards,
	Johannes Ludsteck

||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Roger Wilson wrote:
I have a function b which I define explicitly for a set of input values.

a[1]=4; a[2]=5; a["cat"]="sam";

If I type ??a I get the usual listing of the definition of a but how do I
get a list of the values or patterns for which a is defined?

ListOfPatternsDefined[a]
{1,2,"cat"}

In python this would be a.keys() but in Mathematica I'm stuck?

Any ideas?

Roger.


Johannes Ludsteck
Centre for European Economic Research (ZEW)
Department of Labour Economics,
Human Resources and Social Policy
Phone (+49)(0)621/1235-157
Fax (+49)(0)621/1235-225

P.O.Box 103443
D-68034 Mannheim
GERMANY

Email: ludsteck at zew.de


  • Prev by Date: Re: FixedPoint vs. FixedPointList
  • Next by Date: Re: Need to reduce 2 lists so that only {x,y} pairs with same x remain
  • Previous by thread: Re: List of patterns for which a function is defined?
  • Next by thread: Re: List of patterns for which a function is defined?