Example of structure-processing: complex numbers.


next up previous contents
Next: Example of list processing
Up: Introduction to Prolog for Mathematicians
Previous: Program components

Example of structure-processing: complex numbers.

add( complex(A,B), complex(C,D),
     complex(E,F)
   ) :-
    E is A+C,
    F is B+D.

multiply( complex(A,B), complex(C,D),
          complex(E,F)
        ) :-
    E is A*C - B*D,
    F is A*D + B*C.

Read this as:

The sum of (A,B) and (C,D) is (E,F) if
    E is the numerical value of A+C and
    F is the numerical value of B+D.

For this to work, A,B,C,D must be numbers. is does arithmetic. Logically speaking, X is Y means ``X is the numerical value of the expression Y''.



Jocelyn Ireson-Ireson-Paine
Mon Jul 17 22:27:41 BST 1995