If I wanted to allow "2*x + 1
" as a correct version of "2x + 1", I could use the line
$student =~ s/*//g;
to remove the *
symbol, am I right?
No. The
*
is a special symbol in regular expressions (that's the name of the pattern-matching mechanism for substitutions like this). It means "the previous thing repeated 0 or more times", but since there is no repeated thing, this is an error. For example,
/a*/
would match any number (including 0) of a's, while
/[0-9]*/
would be any number of digits (and possibly none).
To get a literal asterisk, you need to quote it, usually with a backslash in perl. E.g., /\*/
. But in PG, backslashes are doubled automatically (so that you don't have to double them by hand for TeX commands). That means /\*/
would become /\\*/
, which is represents any number of backslashes in a row (including none). That's not what you want. So PG uses ~~
to represent a single backslash (sigh). That means you need to use
$student =~ s/~~*//;
to remove asterisks.
I don't think this is really what you want to do, however. That will make some incorrect answers into correct ones. For example, if the correct answer is 6x
then 6**x
(six to the x
power) will be marked correct, as will 6*x*
, which is not even a syntactically correct expression.
Furthermore, some correct answers will be marked incorrect. E.g., 3*2*x
(a correct answer) will become 32x
(an incorrect one).
This type of pattern-based substitution is fraught with pitfalls. Evaluating these types of answers as strings is the wrong approach, and you are just going to get into trouble trying to do it this way.
- You are not going to be able to handle all the correct answers in a reasonable way (the number of permutations is too great).
- You will mark some correct answers incorrect and some incorrect answers correct (as shown above).
- You will not get the usual syntax checking on the expressions, or the helpful error messages when students enter invalid equations.
- Your questions will work substantially differently from all the other WeBWorK problems you may assign (that do proper syntax checking).
- Even if you do provide some error messages, these will not be consistent with the ones produced by MathObjects, which can be a source of needless confusion for students.
You may think that this is requiring clarity, but students will see it as either broken or arbitrary unless you are very specific about the format you are expecting. And I don't see how you can be without giving away the answer. Saying it is the "clearest" or "simplest" form is not sufficient, in my opinion.
Your sense of what is clear or simple is something that you have developed over many years of mathematical experience. That is experience that calculus students don't have, and do not achieve over night. Unless your problems give a lot of support about obtaining the desired format (in terms of syntax messages and hints when their format isn't correct), I think you have a recipe for disaster.
While it is possible to write context's that do provide that type of support, it is not trivial, and you have to be very explicit about what is allowed and what isn't. Since that probably varies from problem to problem, it is usually not practical to go that way.
WeBWorK's philosophy has always been that equivalent answers are correct, even if they are written in an unusual way. The student answer is restricted only for specific situations (e.g., factoring a polynomial, giving an answer as a fraction rather than a decimal, etc.), and then only when it is made clear to the student what that format is. Students who use WeBWorK will have come to expect that. If you intend to change that paradigm, you are going to have to be very clear with your students about that.