[system] / trunk / pg / macros / answerVariableList.pl Repository:
ViewVC logotype

View of /trunk/pg/macros/answerVariableList.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2733 - (download) (as text) (annotate)
Sat Sep 4 20:45:31 2004 UTC (15 years, 5 months ago) by dpvc
File size: 1938 byte(s)
This file defined an answer checker for lists of variable names
(ordered or unordered, and optionally enclosed in parentheses).  You
can use this answer checker for questions that give a system of
equations and ask which variables are the dependent and which the
independent variables, for example.

    1 loadMacros('Parser.pl');
    2 
    3 sub _answerVariableList_init {}; # don't reload this file
    4 
    5 ######################################################################
    6 #
    7 #  This answer checker compares the student answer to a list of
    8 #  variable names (so, for example, you can ask for what values a
    9 #  given function depends on).
   10 #
   11 #  Use addVariables() to create the list of variables that from which
   12 #  the student can choose, and then use variable_cmp() to generate the
   13 #  answer checker.  If the formula passed to variable_cmp contains
   14 #  parentheses around the list, then the student's answer must as
   15 #  well.
   16 #
   17 #  You can also include additional parameters to variable_cmp.  These
   18 #  can be any of the flags appropriate for List() answer checker.
   19 #
   20 #  Usage examples:
   21 #
   22 # addVariables('x','y','z');
   23 # ANS(variable_cmp("(x,y)"));
   24 #
   25 # addVariables('x','y','z','s','t,);
   26 # ANS(variable_cmp("s,t"));
   27 #
   28 # addVariables('x','y','z');
   29 # ANS(variable_cmp("(x)",showHints=>0,showLengthHints=>0));
   30 #
   31 
   32 #
   33 #  A new context for variable lists
   34 #
   35 $context{VariableList} = Parser::Context->new(
   36   operators => {',' => Context()->operators->get(',')},
   37   lists => {'List'  => {class =>'Parser::List::List'}},
   38   parens => {
   39    '(' => {close => ')', type => 'List', formList => 1},
   40    'start' => {close => 'start', type => 'List', formList => 1,
   41                removable => 1, emptyOK => 1, hidden => 1},
   42    'list'  => {type => 'List', hidden => 1},
   43   },
   44   flags => {
   45     NumberCheck =>
   46       sub {shift->Error("Entries in your list must be variable names")},
   47   },
   48 );
   49 Context("VariableList");
   50 
   51 #
   52 #  A shell that calls Formula()->cmp with the right defaults
   53 #
   54 sub variable_cmp {
   55   Value::Formula->new(shift)->cmp(
   56     ordered => 1,
   57     entry_type =>'a variable',
   58     list_type => 'a list',
   59     @_
   60   );
   61 }
   62 
   63 #
   64 #  Handy routine to set up variables
   65 #
   66 sub addVariables {
   67   my $context = Context();
   68   foreach my $v (@_) {$context->variables->add($v=>'Real')}
   69 }
   70 
   71 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9