Marco A.
Marco A.

Reputation: 43662

Prolog custom operator to evaluate

I expect the following code to print out [9 4], but this isn't working

:- op(20,xfx,i).

i(X,Y, Z) :-
         Z=[X,Y].

main:-
       RESULT is 9 i 4, write(RESULT).  

Where am I getting wrong?

Upvotes: 1

Views: 448

Answers (2)

joel76
joel76

Reputation: 5615

this works

:- op(800,xfx,i).

R is A i B :-
    i(A, B, R).

i(X,Y, Z) :-
    Z=[X,Y].

main:-
   RESULT is 9 i 4, write(RESULT).

Upvotes: 2

thanos
thanos

Reputation: 5858

an operator is basically syntactic sugar; instead of writing +(1,2) we simply write 1+2.
therefore, 9 i 4 is equivalent to i(9,4)

now, +/2 is not only an operator but also an arithmetic function
note that the result should be a number so you cannot use it to return a list (and cannot use is/2 either)

Upvotes: 3

Related Questions