Syntax error: Operator expected in my code

Viewed 40

I am a beginner at Prolog so I got this piece of code but I don't understand why it doesn't work

max([Max],Max).
max([H1,H2|T],Max):- H1 > H2, max([H1|T],Max).
max([H1,H2|T],Max):- H1 < H2, max([H2|T],Max).

pozitia([X|_], X, 1).
pozitia([_|Y], X, I) :- pozitia(Y, X, I2), I is I2 + 1.

questions

max([1,2,3,4,5,7,2],X).
pozitia([1,2,3,4,5,3,2,1,1,2,3,4], 4, Pozitia).

The error I got is Syntax error: Operator expected.

I don't understand why!

1 Answers

Syntactically, if you replace question with question. (add a dot), your code will work, prolog could not figure out anything about question whether it is a fact or a rule.

Logically, you have to comment out the last 3 lines!

max([Max],Max).
max([H1,H2|T],Max):- H1 > H2, max([H1|T],Max).
max([H1,H2|T],Max):- H1 < H2, max([H2|T],Max).

pozitia([X|_], X, 1).
pozitia([_|Y], X, I) :- pozitia(Y, X, I2), I is I2 + 1.

% questions
% max([1,2,3,4,5,7,2],X).
% pozitia([1,2,3,4,5,3,2,1,1,2,3,4], 4, Pozitia).

These are some queries to test max and pozitia rules.

enter image description here

Related