The expression I want to match obey a simple pattern that repeats it self a number of times.
f[a]@f[b]@f[c]@...@f[X]
How would you match all expressions of this form?
|
The expression I want to match obey a simple pattern that repeats it self a number of times.
How would you match all expressions of this form? |
|||||||
|
New method
Old methods for referenceIf subexpression evaluation is not a concern:
To address Leonid's critique that this has evaluation leaks one might instead write:
Now it works here too:
|
||||
|
|
We can match expressions of the desired form thus:
This assumes that the transformation rule
This variation ensures that each iteration operates only upon the surface structure of the expression. And should some
|
|||||||||||||
|
|
@Mr.Wizard already picked the sweetest answer, but here is my version:
which is basically the same idea, but may be easier (or harder) to comprehend than his solution, depending on how you think. The pattern-matching is done as
Note that, if evaluation is not an issue, the above code can be simplified:
|
|||||||||||||
|
|
Leonid and Mr.Wizard have given you good answers. Here's a highly unconventional solution that works when
Some examples:
|
|||||||||||
|