Given two operands and
which are real numbers, define the operators,
,
,
,
,
,
,
,
with respect to arithmetic operations such as addition, subtraction, multiplication, division, exponentiation, rooting, and Modulo.
1 2 3 4 5 6 7 8 9 10 11 |
+------+------------+-----+-----+-------------------------+ | ID | operator | a | b | instance for example | +------+------------+-----+-----+-------------------------+ | 1 | + | 2 | 3 | a + b = 2 + 3 = 5 | | 2 | - | 5 | 2 | a - b = 5 - 2 = 3 | | 3 | × | 3 | 2 | a * b = 3 * 2 = 6 | | 4 | ÷ | 9 | 3 | a / b = 9 / 3 = 3 | | 5 | ^ | 3 | 2 | a ^ b = 3 ^ 2 = 9 | | 6 | √ | 3 | 8 | a √ b = 3 √ 8 = 2 | | 7 | mod | 7 | 4 | a mod b = 7 mod 4 = 3 | +------+------ -----+-----+-----+-------------------------+ |
Not so accustomed to these, Lisp cannot interpret or compile them until we have defined those seven primitive functions, better known as symbolic computations; as were newborn infants taught human intelligence by grownup adults between homo sapiens.
To Lisp, arithmetic operations are usually predefined. We have in our toolkit
three mathematical functors, i.e., list, iteration, and recursion.
The roots of quadratic equation are given by the formula:
the expected outcome of which is calculated by parts, first the discriminant ‘delta’:
1 |
(define (delta a b c) (- (square b) (* 4 a c))) |
;
then the plus-minus ‘pm’:
1 2 |
(define (pm u v) (values (cons (+ u v) (- u v)))) |
;
and hence
1 |
(/ (pm (- b) (sqrt (delta a b c))) (* a 2)) |
will solve for real roots, ignoring the complex though.
Predefining the seven primitive functions is in order for solving any computational problems by Lisp.
(to be continued)