Adventures in Prolog - Simplify Sum (2)
The last post showed how to compute simple addition expressions. To provide a more structured approach to the problem at hand, I decided to write a small Prolog program that actually recognizes such addition expressions. This program basically represents a Prolog conversion of the grammar underlying such addition expressions. Because Prolog notation and grammer are so close, I will not comment on the following Prolog clauses. addition_expression (A) :- sum_term (A). addition_expression (A) :- A = X + Y, addition_expression (X), sum_term (Y). sum_term (X) :- number (X). sum_term (X) :- atom (X). sum_term (X) :- prod_term (X). prod_term (X) :- X = A * B, number (A), atom (B). prod_term (X) :- X = A * B, atom (A), number (B).