DEV Community

Discussion on: Algebra Expression Simplifier

Collapse
 
pentacular profile image
pentacular

Very nice.

You might make it a lot simpler by restructuring it as a set of more regular rewrite rules operating on a structured representation.

Collapse
 
i8sumpi profile image
Kira L

Thanks a ton for the advice! Do you mean that it would have been simpler first turn it into an expression tree, then simplify it?

Collapse
 
pentacular profile image
pentacular • Edited

Yes, I think so.

Perhaps even a representation as simple as:

['*', 3, ['+', 3, 2]]

Then you can imagine a recursive walk over that expression using a rule that matches

[op('+'), number, number]

and rewrites it to the sum, reducing that to

['*', 3, 5]

, and another rule like

[op('*'), number, number]

and reduces that to 15, and then you're done.

Thread Thread
 
i8sumpi profile image
Kira L

Great idea, thanks!