f(n)
before WeBWorK would be able to handle it.
It is possible to use the parserFunction.pl
macro library to do this, though it does present some possible (but unlikely) ways to get the answer marked correct without it being what you want.
Here is a sample code snippet that may do what you are after:
loadMacros("parserFunction.pl");
Context("Numeric")->variables->are(n=>"Real");
parserFunction(f => "sin(pi^n)+e"); # something student is unlikely to type by hand
$f = Formula("3 f(n-1) + 2");
BEGIN_TEXT
\(f(n)\) = \{ans_rule(20)\}.
END_TEXT
ANS($f->cmp);
Note that this defines
f(n)
to be something obscure that a student is unlikely to type by hand so that WeBWorK can evaluate the formula. It would be difficult to get a formula that matched without writing it using f
(though if the definition of f
is chosen badly, it might be able to be written in several ways using f
).
Unfortunately, you can't include the f(n)=
as part of the answer, even using the parserAssignment.pl
macros, as they don't allow recursive definitions.
Alternatively, you could write a custom checker that looks for the specific algebraic structure you are using, but that would limit the form that the student could use to enter the answer (e.g., 3 f(n-1)+1+1
might not be accepted if you weren't careful about how you code your checker). This approach is difficult, and I wouldn't recommend it.
Davide