Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog - Variable as operator

Tags:

prolog

I have an operator stored in a variable Op and two integers are stored in X and Y. Now, I want to do something like (Z is X Op Y), but this syntax seems not to be correct.

Does anybody know if there is a way to do this in Prolog?

Thanks for your answers

like image 209
Jean Gauthier Avatar asked Oct 01 '11 11:10

Jean Gauthier


People also ask

What does == mean in Prolog?

The == operator is similar to equality operator = but with vital difference. If Term1 and Term2 unify, the Term1==Term2 succeeds. For example: ?- likes(A, mary) = likes(prolog, B).

How do you use or operator in Prolog?

Performing an "or" in Prolog can also be done with the "disjunct" operator or semi-colon: registered(X, Y) :- X = ct101; X = ct102; X = ct103. Save this answer.

What does =:= mean in Prolog?

=:= expression is meaning of exactly equal. such as in JavaScript you can use === to also see if the type of the variables are same. Basically it's same logic but =:= is used in functional languages as Prolog, Erlang.

What does operator mean in Prolog?

notation contains a number of arguments in parenthesis like likes(hary, jack). Any user-defined predicate which has two arguments can be converted into an infix operator as an alternative. In this, we can write the functor between the two arguments, and they have no parenthesis like. hary likes jack.


1 Answers

you can do it by building the predicate using the =.. operator.

try it like:

compute(X,Y,Op,Z) :-
   Eq=..[Op, X, Y],
   Z is Eq.

An operator is really just like any other functor.

like image 156
DaveEdelstein Avatar answered Oct 06 '22 20:10

DaveEdelstein