I am trying to write prolog code to select a random rule from a list and then execute it. This is how far I have got it to work:
/* Rules */
rule1 :- write('1').
rule2 :- write('2').
rule3 :- write('3').
/* List of rules */
list([rule1,rule2,rule3]).
/* Rule to select random rule from list */
findRule :- random_between(0,2,Elem),
list(L),
nth0(Elem,L,Rule),
Rule.
Produces (for example) this outcome:
|: findRule.
2
true.
However, I would like to pass a parameter to the rule such as:
/* Rules */
rule1(X) :- write(X), write('1').
rule2(X) :- write(X), write('2').
rule3(X) :- write(X), write('3').
/* List of rules */
list([rule1,rule2,rule3]).
/* Rule to select random rule from list */
findRule(X) :- random_between(0,2,Elem),
list(L),
nth0(Elem,L,Rule),
Rule(X).
To produce an outcome like this:
|: findRule(hallo).
hallo2
true.
This does not work. I already thought about trying the same thing with a dynamic list but I wanted to check first, if there might be a simpler solution to the problem..
Use call/2
in order to satisfy the last goal:
findRule(X):-
random_between(0,2,Elem),
list(L),
nth0(Elem,L,Rule),
call(Rule,X).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments