In your program you are mixing querying via the toplevel shell and performing side-effects with write/1
. In this manner you will most probably miss a lot of what Prolog can offer you.
Instead, stick to pure side-effect free relations and let all writing be done by the toplevel.
Thus, instead of suggest/1
, consider a new relation, say biganimal_compatiblewith/2
.
biganimal_compatiblewith(X,Y) :-
bigAnimal(X),
bigAnimal(Y),
coexist(X,Y),
dif(X,Y). % maybe
?- biganimal_compatiblewith(X,Y).
X = melan, Y = narty
; X = melman, Y = nelman
; ... .
Now, both X
and Y
are printed on the toplevel. Think how useful this relation is compared to the original suggest/1
. You can ask for one or the other, or you even can ask
?- biganimal_compatiblewith(X,X).
You can also reuse this relation, building more complex ones:
?- biganimal_compatiblewith(X,Y), burgervore(Y).
That is the essence of Prolog relations. You can spare out side effects for quite some time.
For another example how to avoid unnecessary side effects: What are the pros and cons of using manual list iteration vs recursion through fail