Prolog

summation and factorial in gprolog

by Forrest Sheng Bao http://fsbao.net

I can't run sicstus Prolog on my department Solaris workstation. That makes me very unhappy at 3 am. So I decide to use gprolog. And the programming can be easier. In gprolog, FD is built-in.

Factorial in gprolog:

factorial(0,1). 

factorial(N,F) :-  
   N>0, 
   N1 is N-1, 
   factorial(N1,F1), 
   F is N * F1.

Summation in gprolog

sum(0,0). 

sum(N,F) :-  
   N>0, 
   N1 is N-1, 
   sum(N1,F1), 
   F is N + F1.

It seems that "is" is also a built-in predicate in swi-prolog.

domains, constraints and labeling in Sicstus Prolog solver, finite domain

by Forrest Sheng Bao http://fsbao.net

It's really tricky to program in Sicstus Prolog solver.

Bellow is an example

:- use_module(library(clpfd)).
a(X,Y):-domain([X], 1,4), domain([Y], 4, 6), X+2#>Y, labeling([],[X,Y]).

Results on Sicstus:

| ?- [d].
% consulting /home/grad2/fsbao/d.pl...
% consulted /home/grad2/fsbao/d.pl in module user, 10 msec 72 bytes
yes
| ?- a(X,Y).
X = 3,
Y = 4 ? ;
X = 4,
Y = 4 ? ;
X = 4,
Y = 5 ? ;
no

Ref: Constraint Logic Programming over Finite Domains, Sicstus Manual, http://www.sics.se/sicstus/docs/3.7.1/html/sicstus_33.html

Syndicate content