9
votes
Accepted
Why does this pattern not match?
The first one matches because HoldPattern just prevents the pattern from evaluation, so it is equivalent to
...
9
votes
Apply function to all first arguments of a nested list of rules
list = {1 -> a, 2 -> {3 -> c, 4 -> d, 5 -> {6 -> f, 7 -> {8 -> h, 9 -> j}}}};
1. MapAt
...
8
votes
7
votes
Why does Blank sometimes not match the empty expression?
One source of confusion here is about what MatchQ[, _] actually does. Contrary to what you think, the first argument of MatchQ here is not "the empty ...
6
votes
Why does Blank sometimes not match the empty expression?
Your pattern is actually interpreted as a multiplication:
FullForm[_{_Symbol}]
(* List[Times[Blank, Blank[Symbol]]] *)
To match a list consisting of a Symbol, use ...
6
votes
Accepted
Switch with string patterns
Switch doesn't use the form of the first argument to somehow predict what kind of comparison you want to do. So, it doesn't automatically try a string match just ...
5
votes
Apply function to all first arguments of a nested list of rules
You could first find all the positions of Rule, and change the last index to 1:
...
5
votes
Apply function to all first arguments of a nested list of rules
the rules to get something of the form
lst2={z[1]->a,z[2]->{z[3]->c,z[4]->d,z[5]->{z[6]->f,z[7]->{z[8]->h,...}}}}.
how about
...
5
votes
Accepted
5
votes
Apply function to all first arguments of a nested list of rules
lst = {1 -> a, 2 -> {3 -> c, 4 -> d, 5 -> {6 -> f, 7 -> {8 -> h}}}}
lst //. Rule[Except[_z, a_], b_] :> Rule[z @ a, b]
{z[1] -> a, ...
5
votes
Accepted
4
votes
Pattern matching vs. condition in a function
For g4, your condition doesn't evaluate to True or False. Specifically, ...
4
votes
Accepted
ToString[n] in pattern replace does not take the value of n
Output as String
res = Power[x, 3] /. Power[x, n_] :> "xp" <> ToString[n]
"xp3"
Output as ...
4
votes
Accepted
How to apply a transformation to a total derivative?
RuleDelayed (:>) doesn't hold its first argument (it only has the attribute HoldRest), so ...
2
votes
2
votes
Apply function to all first arguments of a nested list of rules
Additional ways to use ReplaceAll and Replace:
...
1
vote
Match a sequence of integers
list =
{{0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0}};
p1 = {0, 0, 1, 0, 0};
p2 = {.5, .5, 1, .5, .5};
Since V 11.3 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
pattern-matching × 1473list-manipulation × 270
replacement × 225
string-manipulation × 166
function-construction × 110
filtering × 77
functions × 66
argument-patterns × 61
rule × 55
expression-manipulation × 54
evaluation × 43
simplifying-expressions × 39
symbolic × 37
bugs × 36
performance-tuning × 35
core-language × 35
programming × 34
associations × 28
matrix × 25
sequence × 25
hold × 24
regular-expressions × 24
attributes × 22
expression-test × 21
calculus-and-analysis × 19