SOOOOO NEWB... Prolog Syntax Error: operator expected

58 views Asked by At

I was trying to make a function that if one is negative, return the other.

foo(X,Y,F):- **X**<=0,F is Y.
foo(X,Y,F):- **Y**<=0,F is X.

is showing error... how to fix this?(** is the highlighted part)

1

There are 1 answers

0
boisvert On

As the comment says; =< not <=:

foo(X,Y,F):- X=<0, F is Y.
foo(X,Y,F):- Y=<0, F is X.

But a few more thoughts.

Rather than is, we can simply use identity:

foo(X,Y,F):- X=<0, F=Y.
foo(X,Y,F):- Y=<0, F=X.

Which has the advantage of working in more cases where not all variables are instantiated. Finally, it can also be written through pattern matching:

foo(X,F,F):- X=<0.
foo(F,Y,F):- Y=<0.