Skip to main content
Improve the expression further (I think)
Source Link
xzczd
  • 71.6k
  • 10
  • 183
  • 524

Is there a function or package visualizing How to visualize pattern matching process?

I want thea function dothat does the same job like ReplaceRepeated, and can make the matching process visible.

I searched the Internet andbut didn't find any existing code or package. So that meets my requirement, so I wrotetried to write one...:

So I can test some exampleIt works in ReplaceRepeatedsome cases:

Example 1:Example 1

Example 2:Example 2

Example 3:Example 3

However, itbut fails in some other cases, for example:

So I want to know howHow to improve my myReplaceRepeated?

will cause problem. So, that's why I add a If function in myReplaceRepeated.

Is there a function or package visualizing pattern matching process?

I want the function do the same job like ReplaceRepeated, and can make the matching process visible.

I searched the Internet and didn't find any package. So I wrote one...

So I can test some example in ReplaceRepeated

Example 1:

Example 2:

Example 3:

However, it fails in some cases, for example:

So I want to know how to improve my myReplaceRepeated

will cause problem. So I add a If function in myReplaceRepeated.

How to visualize pattern matching process?

I want a function that does the same job like ReplaceRepeated, and can make the matching process visible.

I searched the Internet but didn't find any existing code or package that meets my requirement, so I tried to write one:

It works in some cases:

Example 1

Example 2

Example 3

but fails in some other cases:

How to improve my myReplaceRepeated?

will cause problem, that's why I add a If in myReplaceRepeated.

Correct the grammar mistakes and unidiomatic expressions. (I wish I didn't introduce more… )
Source Link
xzczd
  • 71.6k
  • 10
  • 183
  • 524

Is there a function or package that can make Pattern Matching become visualizationvisualizing pattern matching process?

I want the function do the same job like ReplaceRepeatedReplaceRepeated,and and can make the symbol become visiblenessmatching process visible. I search

I searched the Internet and don'tdidn't find any package.So So I writewrote one...

So I can test some example in ReplaceRepeatedReplaceRepeated

exampleExample 1:

exampleExample 2:

exampleExample 3:

But some exampleHowever, it fails in some cases,for for example:

So I want to know how to improve my Program? IfmyReplaceRepeated

Is there is areally no existing tool can make Pattern Matching becomefor the visualization of pattern matching?

I willLet me explain my function more carefully and I want to use thiswith the following 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*short for match*) = rule[[1]]rule[[1]]; 

{Longest[a___], b_, c__, b_, d___}

And then, I want to know the ahow those variables i.e. a,bb,cc and dd in "mat" how to representrule match the sub-listsequence in "lis"? butlis when lis //. rule executes.

To monitor how many varibles in rule? Sothose variables match, I must find these variables...need to first know what variables rule contain:

OkThen I find out how these variables match lis respectively,Next~ This for example the following piece of code means "a" representshows {1,3,1,4}a matches 1, 3, 1, 4 in lislis.   

But remember,theNotice the level is {0}{0},otherwise otherwise we getsget {}{}

Ok,combine theseThese are combined in a loop.

theThe output means "a" represent {1,3,1,4},"b" representa represents {1}{1, 3, 1, 4},"c" representb represents {3,4,2{1},7} c represents {3, 4, 2, 7},the the next "b" meansb represents the forthlatter {1}{1} and the "d" represent {8}d represents {8} in "lis"lis.

Ok,CombineThen I define a testQ to combine all these together and use pure function to illustrate testQ:

Ok.ReplaceRepeated function can be overwriten by FixedPointList.The processThe functionality of FixedPointList can bring a side effect thatReplaceRepeated is always search the a,b,c,d represent what sub-list..achieved by FixedPointList:

FinallyThis is almost the output of example 1,we get except for the similar resultcoloring.

The variables isn'tYou may noticed that the {}. So ifvariables in the above code can't be rule is{}, so a rule like

This is otherwill cause problem.So So I defineadd a IfIf function in myReplaceRepeatedmyReplaceRepeated.And using FixedPointList

So long my code gets the ability to simulatehandle ReplaceRepeatedexample 1 ~ 3.

But, but if the lislis and rule isrule are:

But theMy program don't fit this problemfails,so how to fix it?

Is there a function or package that can make Pattern Matching become visualization?

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...

So I can test some example in ReplaceRepeated

example 1:

example 2:

example 3:

But some example fails,for example:

So I want to know how to improve my Program? If there is a tool can make Pattern Matching become visualization?

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...

Ok,Next~ This code means "a" represent {1,3,1,4} in lis.  But remember,the level is {0},otherwise we gets {}

Ok,combine these in a loop.

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

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..

Finally,we get the similar result.

The variables isn't {}. So if the rule is

This is other problem.So I define a If function in myReplaceRepeated.And using FixedPointList to simulate ReplaceRepeated.

But if the lis and rule is:

But the program don't fit this problem,so how to fix it?

Is there a function or package visualizing pattern matching process?

I want the function do the same job like ReplaceRepeated, and can make the matching process visible.

I searched the Internet and didn't find any package. So I wrote one...

So I can test some example in ReplaceRepeated

Example 1:

Example 2:

Example 3:

However, it fails in some cases, for example:

So I want to know how to improve my myReplaceRepeated

Is there really no existing tool for the visualization of pattern matching?

Let me explain my function with the following example.

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(*short for match*) = rule[[1]]; 

I want to know how those variables i.e. a,b,c and d in rule match the sub-sequence in lis when lis //. rule executes.

To monitor how those variables match, I need to first know what variables rule contain:

Then I find out how these variables match lis respectively, for example the following piece of code shows a matches 1, 3, 1, 4 in lis. 

Notice the level is {0}, otherwise we get {}

These are combined in a loop.

The output means a represents {1, 3, 1, 4},b represents {1}, c represents {3, 4, 2, 7}, the next b represents the latter {1} and d represents {8} in lis.

Then I define a testQ to combine all these together:

The functionality of ReplaceRepeated is achieved by FixedPointList:

This is almost the output of example 1, except for the coloring.

You may noticed that the variables in the above code can't be {}, so a rule like

will cause problem. So I add a If function in myReplaceRepeated.

So long my code gets the ability to handle example 1 ~ 3, but if lis and rule are:

My program fails, how to fix it?

added 411 characters in body
Source Link
partida
  • 6.9k
  • 25
  • 51

But if the lis and rule is:

lis = f[a][b][c][d]; rule = g_[x_][y__] -> g[x, y]; mat = rule[[1]]; variables = ToExpression@ StringCases[ ToString[mat], (vars : WordCharacter ..) ~~ "_" | "__" | "___" :> vars]!={} 

True

But the program don't fit this problem,so how to fix it?

But if the lis and rule is:

lis = f[a][b][c][d]; rule = g_[x_][y__] -> g[x, y]; mat = rule[[1]]; variables = ToExpression@ StringCases[ ToString[mat], (vars : WordCharacter ..) ~~ "_" | "__" | "___" :> vars]!={} 

True

But the program don't fit this problem,so how to fix it?

added 411 characters in body
Source Link
partida
  • 6.9k
  • 25
  • 51
Loading
edited tags
Link
partida
  • 6.9k
  • 25
  • 51
Loading
added 28 characters in body; added 8 characters in body
Source Link
partida
  • 6.9k
  • 25
  • 51
Loading
added 2184 characters in body
Source Link
partida
  • 6.9k
  • 25
  • 51
Loading
Tweeted twitter.com/#!/StackMma/status/565104990575140864
edited tags
Link
m_goldberg
  • 108.6k
  • 16
  • 107
  • 263
Loading
added 66 characters in body
Source Link
partida
  • 6.9k
  • 25
  • 51
Loading
Source Link
partida
  • 6.9k
  • 25
  • 51
Loading