About the rules in the prologue.,

In my program, I have some rules, for example:

tellme(X) :- knows(X). tellme(friends1(X)) :- tellme(X). tellme(friends2(X)) :- tellme(X). tellme(friends3(X)) :- tellme(X). . . . tellme(friends25(X)) :- tellme(X). 

Now these friends1, friends2, friends3 ..... depend on N, which is a variable. Here, for example, the value of my N is 25. Thus, you can write a rule to generate these rules to N, or I need to manually write these rules for N times. Any suggestions or solutions are welcome. Thank you so much for your attention.

+4
source share
2 answers

You can create any kind and amount of code in the most modern Prolog environment using the term extension , with some help the universal operator . The following example works for SWI:

 term_expansion(gen_tellme(N), Terms) :- findall((tellme(F) :- tellme(X)), (between(1, N, I), atom_concat(friend, I, Fi), F =.. [Fi, X]), Terms). gen_tellme(25). % generates 25 copies of the tellme clause. 

However, embedding information in a predicate name, that is, a friend’s number, is usually not a good design. Why not rewrite the code with friend(N, X) , where N is the number?

+2
source

You may have some success with the friends(N,X) binary functor instead of a single functor on N You can modify tellme and knows to take two arguments. ( knows(X) has no direct meaning for me, so I do not fully understand what you need.)

If for some reason this is not what you want, you can create a new functor with something like ( untested )

 friends_functor(N, Functor) :- number(N), atom_concat(friend, N, Functor). 

Then use asserta or assertz . I really don’t understand why you want to do this.

+1
source

Source: https://habr.com/ru/post/1343515/


All Articles