MathGroup Archive 2008

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

Search the Archive

Matching HoldPattern explicitly


I'm trying to use Mathematica symbols as dictionaries. In particular
if I have (kk[1, 2] = 3; kk[3, 4] = 4;), I'd like a function "keys"
that would accept kk, and return {{1,2},{3,4}}.

A recipie on http://www.physic.ut.ee/~kkannike/english/prog/mathematica/index.html
suggests to do

keys[dict_] := Map[#[[1]] /. {HoldPattern[dict[x_]] -> x} &,
DownValues[dict]]

However, this doesn't quite work (p:HoldPattern[x] doesn't match
"HoldPattern[x]"). Verbatim[HoldPattern[dict... doesn't work because I
need to do a substitution for local dict variable. So my question is

1. What's the best way to match HoldPattern explicitly, while allowing
for local variable substitutions inside "HoldPattern" pattern
2. What is the best way of implementing Python like dictionaries in
Mathematica?


  • Prev by Date: Re: Extracting the value from Solve
  • Next by Date: Re: Extracting the value from Solve
  • Previous by thread: Re: NDSolve[] and Differential Equations: Problem solving two
  • Next by thread: Re: Matching HoldPattern explicitly