Query in prolog
I know about that we can add facts dynamic at r开发者_运维百科un time, mean
fact1(+First,+Second).
I can insert using asserta. But I want to ask can i add the functor at run time mean initially there is only a single fact like fact1(first,second).
but user will insert some more facts at run time say
fact2(first,second).
fact3(first,second).
Here initially we don't know how many facts are to be added. Say user will give input as 3 then we have to add fact1,fact2 and fact3. If user gives input as 4 then we have to add four fact i.e. fact1,fact2,fact3 and fact4.
Any suggestions in this direction are appreciated. Thanks.
You can build the fact to assert using the predicates atom_concat/3 and =../2 Here goes an example that uses these to assert a predicate as you need (a base name, a number and two arguments):
assert_predicate(Name, N, Parm1, Parm2):-
atom_concat(Name, N, FactName),
Fact=..[FactName, Parm1, Parm2],
asserta(Fact).
and you would use it with something like: assert_predicate(fact, 1, first, second)
精彩评论