I have a base of knowledge in a Prolog file (.pl) using SWI-Prolog.
reserve(bus(2389),place(4),datetravel([12,2,1999]),
info([88032454321,robert,downe])).
reserve(bus(2389),place(33),datetravel([12,2,1999]),
info([88032454321,alberto,perez])).
reserve(bus(2389),place(16),datetravel([12,2,1999]),
info([88032454321,angel,clark])).
reserve(bus(4566),place(17),datetravel([22,3,2005]),
info([88032454321,don,self])).
reserve(bus(2389),place(22),datetravel([12,2,1999]),
info([88032454321,singmud,dante])).
I implemented a rule that ye gave me several parameters count the number of events that comply with these restrictions, but does not work for me, as I can?
Rule
test(N,D,M,Y,P):-
reserve(bus(N),place(4),datetravel([D,M,Y]),
info([88032454321,robert,downe])),
test(N,D,M,Y,P+1).
Question
?- test(2389,12,2,1999,P).
Given my question the rule should return the number 4.
P+1and expect Prolog to evaluate the expression. It doesn't work that way. it will literally pass the term,'+'(P,1)to the predicate.And you don't have a base case fortestthat handles the trivial case. With just one recursive predicate, when would you expect it to end? And then with each call totest, Prolog will keep matching the same fact to the rule you have (since a new predicate call starts at the beginning for matching facts/rules on a query), so that won't properly count them - lurkerfindallthen take the length of the result:findall( _, reserve(bus(_), place(4), datetravel([_,_,_]), info([88032454321, robert,downe])), L), length(L, N).and thenNis the count. - lurkerfindall/3from SWI's implementation, you can download the SWI source. - lurker