I want the function do the same job like **ReplaceRepeated**,and can make the symbol become visibleness.
I search the Internet and don't find any package.So I write one...
Clear[myReplaceRepeated];
myReplaceRepeated[lis_List, rule_, n_: Infinity] :=
Module[{variables, mat = rule[[1]], data, tem},
variables =
ToExpression@
StringCases[
ToString[mat], (vars : WordCharacter ..) ~~ "_" | "__" | "___" :> vars];
If[variables != {},
testQ = Table[Cases[#1, #2 -> i, {0}], {i, variables}] &;
data =
Reap[FixedPointList[(Sow[testQ[#, mat]]; # /. rule) &, lis,
n]][[2, 1]];
Labeled[Grid[Prepend[data, variables], Frame -> All,
Background -> {Lighter /@
Hue /@ Range[0, 1, 1/Length[variables]]}],
Column[{lis, "the rule is", rule}], Top],
Labeled[
Grid[tem = Most@FixedPointList[# /. rule &, lis, n], Frame -> All,
Background -> {None, {Lighter /@
Hue /@ Range[0, 1, 1/Length[tem]]}}],
Column[{lis, "the rule is", rule}], Top]
]]
So I can test some example in **ReplaceRepeated**
example 1:
myReplaceRepeated[{1, 3, 1, 4, 1, 3, 4, 2, 7, 1, 8},
{Longest[a___], b_, c__, b_, d___} -> {b, b, a, c, d}]
![enter image description here][1]
example 2:
myReplaceRepeated[{1, a, 2, b, 3, c}, _?NumericQ -> F[Infinity], 4](*4 is set the MaxIterations*)
![From "Mathematica Cookbook"][2]
example 3:
myReplaceRepeated[{1, 2}, {a_, b_} -> {{a}, {b}}, 5]](*5 is also set the MaxIterations*)
![From "Mathematica Cookbook"][3]
But some example fails,for example:
myReplaceRepeated[f[a][b][c][d], g_[x_][y__] -> g[x, y]]
myReplaceRepeated[Log[Sqrt[a (b c^d)^e]], {Log[x_ y_] :> Log[x] + Log[y], Log[x_^k_] :> k Log[x]}]
So I want to know how to improve my Program?
If there is a tool can make Pattern Matching become visualization?
***Edit:***
I will explain my function more carefully and I want to use this example.
First,define two things,one is the *expr* and the other is *rule*
lis = {1, 3, 1, 4, 1, 3, 4, 2, 7, 1, 8};
rule = {Longest[a___], b_, c__, b_, d___} -> {b, b, a, c, d};
mat(*means match*) = rule[[1]]
> {Longest[a___], b_, c__, b_, d___}
And then, I want to know the a,b,c and d in "mat" how to represent the sub-list in "lis"?
but how many varibles in rule? So I must find these variables...
variables =
ToExpression@StringCases[
ToString[mat], (vars : WordCharacter ..) ~~ "_" | "__" | "___" :> vars]
> {a, b, c, b, d}
Ok,Next~ This code means "a" represent {1,3,1,4} in lis.
**But remember,the level is {0},otherwise we gets {}**
Cases[{1, 3, 1, 4, 1, 3, 4, 2, 7, 1,
8}, {Longest[a___], b_, c__, b_, d___} -> a, {0}]
> {1, 3, 1, 4}
Ok,combine these in a loop.
Table[Cases[{1, 3, 1, 4, 1, 3, 4, 2, 7, 1,
8}, {Longest[a___], b_, c__, b_, d___} -> i, {0}], {i, variables}]
> {{1, 3, 1, 4}, {1}, {3, 4, 2, 7}, {1}, {8}}
the output means "a" represent {1,3,1,4},"b" represent {1},"c" represent {3,4,2,7},the next "b" means the forth {1} and the "d" represent {8} in "lis".
Ok,Combine these together and use pure function to illustrate **testQ**
testQ = Module[{variables},
variables =
ToExpression@
StringCases[
ToString[#2], (vars : WordCharacter ..) ~~
"_" | "__" | "___" :> vars];
Table[Cases[#1, #2 -> i, {0}], {i, variables}]] &;
testQ[lis, mat]
> {{1, 3, 1, 4}, {1}, {3, 4, 2, 7}, {1}, {8}}
Ok.**ReplaceRepeated** function can be overwriten by **FixedPointList**.The process of FixedPointList can bring a side effect that is always search the a,b,c,d represent what sub-list..
data = Reap[
FixedPointList[(Sow[
testQ[#,
mat]]; # /. {Longest[a___], b_, c__, b_, d___} -> {b, b, a, c,
d}) &, {1, 3, 1, 4, 1, 3, 4, 2, 7, 1, 8}]][[2, 1]];
Grid@data
> ![enter image description here][4]
Finally,we get the similar result.
[1]: https://i.sstatic.net/VCnMC.png
[2]: https://i.sstatic.net/dXby5.png
[3]: https://i.sstatic.net/9gzwN.png
[4]: https://i.sstatic.net/US0vv.png