WeBWorK Problems

Creating a List from a MathObject tree

Creating a List from a MathObject tree

by Jaimos Skriletz -
Number of replies: 1

I am looking for a way to parse the MathObject tree and create a list for a given outer most operation (though my use case is currently + and -).

For instance I want to take a student answer of the form `A+B+C+D` and turn it into a List (A,B,C,D) that I can compare to an answer key (either ordered or unordered).

The use case I have for this is partial fractions and Taylor polynomials, in which I want to compare their answers as if it were a list of terms. I do know partial fractions can mostly be done with bizarroArithmetic.pl and Taylor polynomials withcontextLimitedPolynomial.pl (though this doesn't have an option to force the order of addition which I will sometimes want to do). But I find limitations with both where just being able to get a list of terms would be useful to me. (Also be nice if this were a bit more general so I could also use it to get a list of factors with the operation * and /).

I have attempted this, but I get lost in the tree after a single operation, so I have been able to turn `A+B` into a list, but have trouble with `A+B+C` depending on how I group it, `(A+B)+C` vs `A+(B+C)`.

Thanks in advanced for any suggestions on how to get a list of terms or factors from a students answer.

In reply to Jaimos Skriletz

Re: Creating a List from a MathObject tree

by Christopher Heckman -
If I was writing my own function, I'd go with recursion because it'd result in the shortest code. I assume that that's what you're doing.

You're not saying what your trouble is for A+B+C, though. Are you getting List(List(A,B),List(C)) instead of List(A,B,C) ? If so, you should "add" or "dot" the two lists instead of doing whatever you're doing. (Source: the "Construction" section of List's wiki page)