# Logical Formulae

Since the COVID-19 crisis and the university moving to online instruction, I've spent too much time programming WeBWorK assignments. The main problem with programming WeBWorK assignments is that you have to write in Perl; I am conversant in the language as long as you let me Google some of its syntax, but it is far from my favourite language to use.

But sometimes when doing something quick and easy, Perl's reputation as a "write-only" language has some advantages.

For one of the problems that I coded recently, the question is randomized by 3 binary variables A, B, and C. Since Perl is dynamically typed with automatic implicit type conversion, this just means that the three variables can be treated as if they are integers with values either 0 or 1.

Depending on the variables, the possible correct answers to the problem are 0, 1, 2, and 3 (multiple choice question, and Perl uses 0-indexed array). The mapping is

(A,B,C)  -->  ANS
-----------------
(0,0,0)  -->  0
(0,0,1)  -->  1
(0,1,0)  -->  3
(0,1,1)  -->  2
(1,0,0)  -->  1
(1,0,1)  -->  0
(1,1,0)  -->  2
(1,1,1)  -->  3


I didn't want to go through the trouble of typing out a full if-elsif-else table. So what can I do to save keystrokes? Using binary representations, we can store ANS as a 2-bit value, with the more significant bit set equal to B. And the less significant bit set to equal

XOR(XOR(A,C),B)


So in Perl it would look something like

$ans = ($B << 1) + (($A ^$C) ^ $B);  I know however that most of the other maintainers of WeBWorK question code at my department will be a little less than happy had I written this. So in deference to making the code at least slightly more readable, I settled on: $ans = (1-2*$B) * (($A - $C)**2) + 3 *$B; ##### Willie WY Wong
###### Assistant Professor

My research interests include partial differential equations, geometric analysis, fluid dynamics, and general relativity.