From WeBWorK

Jump to: navigation


NAME

parserFormulaAnyVar.pl - implements formulas that can be entered using any variable (not necessarily the same one used by the author).

DESCRIPTION

This file implements the FormulaAnyVar object, which lets you declare a formula that uses any letter as its variable, and allows the student to type the formula using any variable, not necessarily the same as the one used in the professor's answer. That way, if the correct answer is 2x+1, the student could type 2k+1 or 2z+1, etc., and still have it marked correct. Note that the formula can only include a single variable (since it would be difficult to decide how to match up the variables between the student and professor's answers) if there were more than one.

To use FormulaAnyVar objects, load this macro file at the top of your problem:

        loadMacros("parserFormulaAnyVar.pl");

then create a formula as follows:

        $f = FormulaAnyVar("x sin(x) + 3x^2");

The FormulaAnyVar should work like any normal Formula, and in particular, you use $f->cmp to get its answer checker.

        ANS($f->cmp);

Note that the FormulaAnyVar object creates its own private copy of the current Context (so that it can add variables without affecting the rest of the problem). You should not notice this in general, but if you need to access that context, use $f->{context}. E.g.

        Context($f->{context});

would make the current context the one being used by the FormulaAnyVar, while

        $f->{context}->variables->names

would return a list of the variables in the private context.

The name of the variable used in the FormulaAnyVar object is available as

        $f->{x}

in case you want to use it in error messages, for example.