[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 6058 - (download) (as text) (annotate)
Thu Jun 25 23:28:44 2009 UTC (10 years, 7 months ago) by gage
File size: 3136 byte(s)
syncing pg HEAD with pg2.4.7 on 6/25/2009

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright  2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader$
    5 #
    6 # This program is free software; you can redistribute it and/or modify it under
    7 # the terms of either: (a) the GNU General Public License as published by the
    8 # Free Software Foundation; either version 2, or (at your option) any later
    9 # version, or (b) the "Artistic License" which comes with this package.
   10 #
   11 # This program is distributed in the hope that it will be useful, but WITHOUT
   12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   14 # Artistic License for more details.
   15 ################################################################################
   16 
   17 =head1 NAME
   18 
   19 answerVariableList.pl - Creates answer checkers that compare the student's
   20 answer to a list of variable names.
   21 
   22 =head1 MACROS
   23 
   24 =head2 variable_cmp
   25 
   26  ANS(variable_cmp($var_string, %options))
   27 
   28 This answer checker compares the student answer to a list of
   29 variable names (so, for example, you can ask for what values a
   30 given function depends on).
   31 
   32 Use addVariables() to create the list of variables that from which
   33 the student can choose, and then use variable_cmp() to generate the
   34 answer checker.  If the formula passed to variable_cmp contains
   35 parentheses around the list, then the student's answer must as
   36 well.
   37 
   38 You can also include additional parameters to variable_cmp.  These
   39 can be any of the flags appropriate for List() answer checker.
   40 
   41 Usage examples:
   42 
   43   addVariables('x','y','z');
   44   ANS(variable_cmp("(x,y)"));
   45 
   46   addVariables('x','y','z','s','t,);
   47   ANS(variable_cmp("s,t"));
   48 
   49   addVariables('x','y','z');
   50   ANS(variable_cmp("(x)",showHints=>0,showLengthHints=>0));
   51 
   52 =cut
   53 
   54 loadMacros('MathObjects.pl');
   55 
   56 sub _answerVariableList_init {
   57   #
   58   #  A new context for variable lists
   59   #
   60   $main::context{VariableList} = Parser::Context->new(
   61     operators => {',' => $Parser::Context::Default::context{Full}->operators->get(',')},
   62     lists => {'List'  => {class =>'Parser::List::List'}},
   63     parens => {
   64      '(' => {close => ')', type => 'List', formList => 1},
   65      'start' => {close => 'start', type => 'List', formList => 1,
   66                  removable => 1, emptyOK => 1, hidden => 1},
   67      'list'  => {type => 'List', hidden => 1},
   68     },
   69     flags => {
   70       NumberCheck =>
   71         sub {shift->Error("Entries in your list must be variable names")},
   72       formatStudentAnswer => 'evaluated',  # or 'parsed' or 'reduced'
   73     },
   74   );
   75 
   76   main::Context("VariableList");  ### FIXME:  probably should require author to set this explicitly.
   77 }
   78 
   79 sub variable_cmp {
   80   Value->Package("Formula")->new(shift)->cmp(
   81     ordered => 1,
   82     entry_type =>'a variable',
   83     list_type => 'a list',
   84     implicitList => 0,
   85     @_
   86   );
   87 }
   88 
   89 =head2 addVariables
   90 
   91  addVariables(@vars)
   92 
   93 Adds each string in @vars as a varible to the current context.
   94 
   95 =cut
   96 
   97 sub addVariables {
   98   my $context = Context();
   99   foreach my $v (@_) {$context->variables->add($v=>'Real')}
  100 }
  101 
  102 1;
  103 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9