I have an arithmetic expression containing Var, and I need to match it to the following format:
Step * Var + Offset
If it can be represented in this form, I need to get Step and Offset values. If not, I need to know that too.
So far I tried matching expression trees, i. e.
Expression = ["+", ["*", Var, Step], Offset] % and other variations
and using CLP(FD):
78 ?- 2*Ind+3 #= Step*Ind+Offset, Ind in inf..sup.
Step*Ind#=_A,
2*Ind#=_B,
_B+3#=_C,
_A+Offset#=_C.
However, the first approach would require simplifying expressions (for example, Var+5-3) and handling multiple different cases, and the second does not work as I expected it to.
Am I missing something? Is there a simpler way, or is this problem too complex?
?- Step*Var + Offset = 5*10+20.gives Step = 5, Var = 10, Offset = 20. Prolog terms are not nested lists like your expression tree, they are nested compound terms;?- +(*(Step,Var),Offset) = 5*10+20.Expressionwould be something like["*", Var, 2]). Comparing Prolog terms would not work for expressions that don't follow theStep*Var+Offsetstructure for the same reason (for example?- (2*Var+1)-1 = Step*Var+Offset.is false).