Suppose I have a number X and I want to solve a system of equations, for example Y + Z = X, Z * Y = 1.
Now it has solutions Y = 1 / Z and Z = (sqrt (X * X-4) + X) / 2 or (X- (sqrt (X * X-4))) / 2.
Therefore, I can write in Prolog:
solve(X,Y,Z):- Y is (sqrt(X*X-4)+X)/2, Z is 1/Y.
solve(X,Y,Z):- Y is (X-(sqrt(X*X-4)))/2,Z is 1/Y.
It works.
BUT
this requires most of the preliminary work on my part, in fact, its decision in advance and just ask Prolog to evaluate the answer.
Is there a way to get Z and Y without deciding X in advance?
I can't just write things like
solve(X,Y,Z):- X is Y+Z, Z is 1/Y.
due to an error creating the instance.
source
share