Matching HoldPattern explicitly
- To: mathgroup at smc.vnet.net
- Subject: [mg85779] Matching HoldPattern explicitly
- From: Yaroslav Bulatov <yaroslavvb at gmail.com>
- Date: Fri, 22 Feb 2008 04:59:20 -0500 (EST)
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?