tags:

views:

239

answers:

3
+2  Q: 

'if' in prolog?

Probably a stupid question, but I can't find any documentation anywhere for it. Is there a way to do an if in prolog, e.g. if a variable is 0, then to do some actions (write text to the terminal). An else isn't even needed, but I can't find any implementation of if.

A: 

It looks like you need to use a predicate - see: http://www.cs.otago.ac.nz/cosc347/References/LP/LeedsTutorial/node192.html for example.

Paul R
A: 

Prolog program actually is big condition for "if" with "then" which prints "Goal is reached" and "else" which prints "No sloutions was found". A, Bmeans "A is true and B is true", most of prolog systems will not try to satisfy "B" if "A" is not reachable (i.e. X=3, write('X is 3'),nl will print 'X is 3' when X=3, and will do nothing if X=2).

ony
+4  A: 

A standard prolog predicate will do this.

   isfive(5). 

will evaluate to true if you call it with 5 and fail(return false) if you run it with anything else. For not equal you use \=

isNotEqual(A,B):- A\=B.

Technically it is does not unify, but it is similar to not equal.

Prolog Now is a good website for learning prolog.

Edit: To add another example.

isEqual(A,A). 
stonemetal