// add adds two rational numbers add([n1, d1], [n2, d2]) => normalize([n1*d2 + n2*d1, d1*d2]); // sub subtracts the second rational number from the first sub([n1, d1], [n2, d2]) => normalize([n1*d2 - n2*d1, d1*d2]); // mult multiplies two rational numbers mult([n1, d1], [n2, d2]) => normalize([n1*n2, d1*d2]); // div divides the first number by the second div([n1, d1], [n2, d2]) => normalize([n1*d2, n2*d1]); // normalize normalizes the number, i.e. reduces it to lowest terms normalize([n, d]) => g = gcd(n, d), [n/g, d/g]; // sum adds together the elements of a list. The unit is [0, 1], i.e. 0. sum(L) = reduce(add, [0, 1], L); // prod multiplies the elements of a list. The unit is [1, 1], i.e. 1. prod(L) = reduce(mult, [1, 1], L);