matching nested heads
- To: mathgroup at smc.vnet.net
- Subject: [mg36271] matching nested heads
- From: Sidney Cadot <sidney at science-and-technology.nl>
- Date: Thu, 29 Aug 2002 01:37:55 -0400 (EDT)
- Organization: Delft University of Technology
- Sender: owner-wri-mathgroup at wolfram.com
Hi all, In order to do some transformations on a tree I need to be able to replace an expression with head hdA if and if only its parent has head hdB and its grandparent had head hdC. Furthermore, the item itself and its parent may have any number sibling elements. What I do now is the following. Give the expression: ttexpr = grandparent[ parent1[grandchild2[], grandchild1[], grandchild4[], grandchild1[]], grandchild1[], parent2[grandchild2[], grandchild1[], grandchild4[], grandchild1[]]]; I apply a rule: ttexpr /. { grandparent[left1___, parent1[left2___, grandchild1[], right2___], right1___] -> grandparent[left1, parent1[left2, MATCHED[], right2], right1]} which gives the desired expression: grandparent[parent1[grandchild2[], MATCHED[], grandchild4[], grandchild1[]], grandchild1[], parent2[grandchild2[], grandchild1[], grandchild4[], grandchild1[]]] But I have the feeling that it should be possible to do this more elegantly. Does anybody have an idea in this respect? Best regards, Sidney Cadot
- Follow-Ups:
- Re: matching nested heads
- From: Daniel Lichtblau <danl@wolfram.com>
- Re: matching nested heads