## WeBWorK Main Forum

### Set of tuples ### Set of tuples

by Yoav Freund -
Number of replies: 5
If I understand correctly, the Set mathobject is only for sets of reals.
Is there an easy way to define sets of tuples, things of the type
{(1,2),(1,1),(1,0)} or {(A,1),(A,0),(B,2)} and so on?

Yoav ### Re: Set of tuples

by Davide Cervone -
Well, I suppose it depends on what one means by "easy". It certainly could be done, but it would require creating a new MathObject class to handle objects of this type, and a new parser class to handle them in a formula's parse tree. That is doable, but not well documented, and probably not a project for the first timer. I don't know your perl programming background.

It might be possible to use Lists for this, if you associated braces with the list creation (rather then set creation). That would be pretty straight forward. But since lists can hold anything, there would not be much in the way of error messages about the types of elements in the list (e.g., not helpful messages about having a triple rather than a pair).

Can you say something about how you intend to use this?

Davide ### Re: Set of tuples

by Yoav Freund -
Hi Davide,

I have used PERL in the past, but not object-oriented PERL.

I want to use tuples to describe discrete probability spaces, such as 10 tosses of a coin. I wrote a question that asks the students to write down the product of two sets.

I seem to get a reasonable solution using lists. However, I don't get the
expected behaviour when the student uses braces around the list.

This is the code snippet I use:

$L=List("(R,0),(R,1),(G,0),(G,1),(B,0),(B,1)");$L ->{open} = "{";
$L ->{close} = "}";$L ->{requireParenMatch}=0;

I tried first without the last line, but that did not work. Now it does work, but I still can't have the student write braces around the list.

I have a related question: in PERL the factorial is written as "fact( n )" while in the student answers it is written as "n!". Where is the documentation of what the student can write and how to control it (i.e. add a new function) ?

Yoav ### Re: Set of tuples

by Davide Cervone -
The problem is that you haven't told the parser to use braces to form lists. You do that with the statement:
     Context()->parens->set('{' => {type => 'List'});

Now both you and the students can use braces in your answer stings (rather than trying to force the open and close values yourself). Here is a sample:
   Context()->parens->set("{"=>{type=>'List'});
R => {caseSensitive => 1},
G => {caseSensitive => 1},
B => {caseSensitive => 1},
);

$L=List("{(R,0),(R,1),(G,0),(G,1),(B,0),(B,1)}"); Context()->texStrings; BEGIN_TEXT $$L$$ = \{ans_rule(30)\} END_TEXT Context()->normalStrings; ANS($L->cmp);

Note that I've added the three letters that you used as well, as Strings. You could make them constants if that is more appropriate for your usage.

There is some documentation about this on the wiki, but it is a more advanced topic that doesn't have a lot of coverage.

Davide ### Re: Set of tuples

by Davide Cervone -
Where is the documentation of what the student can write and how to control it (i.e. add a new function) ?

I think the best place is probably the Introduction to Contexts on the Wiki. This was completely rewritten earlier this month, and I hope gives more information about how to modify the details of the contexts, and find out more about them.

For example, the section on functions tells you one way to add functions, and links to an example PG file that works that way. This section also shows how to list the names of all the functions that are available.

To add a function that is written using Perl code, see adding new functions in the advanced Context documentation. An example of doing that can be found in pg/macros/contextIntegerFunctions.pl.

The Context pages on the Wiki include a number of other useful references.

Hope that helps.

Davide 