## WeBWorK Problems

### variable types in contexts

by Alex Jordan -
Number of replies: 2
Is there a way to add a new variable type to a context? Specifically, I am looking to do something like

Context("Fraction")->variables->are(x=>'Fraction');

So that together with parserAssignment.pl, I could have access to Fraction Math Object handling like:

Compute("x=4/6");
might give a feedback message that your fraction is not reduced,

Compute("x=1 1/2");
might say that mixed numbers are not allowed

etc.

### Re: variable types in contexts

by Davide Cervone -
The functionality you are looking for is not controlled by the variable type in the context. You can already add the assignment operation to the Fraction context, and get fractional assignments. The reason you aren't getting the messages you want is because of several factors.

First, the Fraction context doesn't require that factions be reduced.

More important, however, is the fact that the check for the reduced fraction is done in the Fraction answer checker, not in the production of a Fraction object, and when you have an Assignment rather than a plain Fraction, it is the Assignment's answer checker that is being used, not the Fraction's.

Here is one way to get the effect you are looking for:

   loadMacros(
"contextFraction.pl",
"parserAssignment.pl",
);

Context("LimitedFraction")->flags->set(reduceFractions => 0);
parser::Assignment->Allow;

$ans = Compute("x=1/2"); Context()->texStrings; BEGIN_TEXT $$ans$$ is \{$ans->ans_rule\}
END_TEXT
Context()->normalStrings;

ANS($ans->cmp->withPostFilter(sub { my$ansHash = shift;
if ($ansHash->{score}) { my ($cvar,$cfrac) =$ansHash->{correct_value}->value;  # get the variable and fraction
my ($svar,$sfrac) = $ansHash->{student_value}->value; # get the variable and fraction my$check = $cfrac->cmp->evaluate($sfrac->string);    # do a full answer check
$ansHash->{score} =$check->{score};                           # transfer the score
$ansHash->{ans_message} =$check->{ans_message};   # and any message
}
return \$ansHash;
}));

Here we use a post-filter on the answer checker to get the correct and student fractions out of the assignments, and perform a new answer check on the fraction parts separately. This will produce the "not reduced" message, if needed, and we copy the score and message into the original answer hash.

Hope that helps.

Davide