I'm curious if Logic Programs can do algebra

前端 未结 4 1448
陌清茗
陌清茗 2021-01-02 06:32

I read a brief article about Prolog and Logic Programming. I\'m curious if Logic Programs can do algebra. Like would you be able to ask what the Variable of X is in the equa

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-02 07:14

    How about this? Note that this will only work for X+Y=Z.

    equation(X,Y,Z):- var(X),X is Z-Y.
    equation(X,Y,Z):- var(Y),Y is Z-X.
    equation(X,Y,Z):- var(Z),Z is X+Y.
    

    You can ask:

    equation(5,X,7).
    X = 2 .
    ?- equation(2,5,X).
    X = 7.
    ?- equation(X,5,7).
    X = 2
    

提交回复
热议问题