Factorial in Prolog
This example consists of two parts — the first part of code should be stored in a file
fact.pl placed in working folder of Poplog, and the second one has to be entered manually.
[-fact]. downloads facts and rules from this file to current Prolog session (and outputs
fact reconsulted to note that download succeeded). Query
fact(16,X). tries to find value of X for which this predicate will evaluate to true. The output required in the example is side effect of query evaluation, and the actual result will be
X = 20922789888000 ?. This means that if you’re not satisfied with this binding, you can reject it (by entering ;), and the search for better binding will continue.
% fact.pl fact(X, F) :- ( X=0, F=1; Y is X-1, fact(Y, Z), F is X*Z), write(X), write('! = '), write(F), nl. % interactive [-fact]. fact(16,X).