Prolog: Select rule randomly from list and pass parameter

ec-m

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..

Tudor Berariu

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Prolog: Select rule randomly from list and pass parameter

From Java

How to randomly select an item from a list?

From Dev

randomly select a color from a premade list

From Dev

Select Randomly an object from a list (Image)

From Dev

Prolog beginner. How to take list as parameter and pass it on

From Dev

Is there a way to pass a compound term as a parameter from a form in swi-prolog

From Dev

Crawl iterating with a parameter over a list of urls from a rule with Scrapy

From Dev

How to select randomly from a list of string-array

From Dev

how to randomly select a certain number of elements from a list

From Dev

How do I randomly select a variable from a list, and then modify it in python?

From Dev

Python selenium: Randomly select from a url list textfile

From Dev

How can I select integers from a list in Prolog?

From Dev

how to pass a list in a predicate in prolog

From Dev

mod_rewrite rule not working (pass parameter from one url to another)

From Dev

Pass parameter from one select function to another using onchange event

From Dev

Rails - pass a parameter to controller from form select dropdown

From Dev

I want to randomly select items from a list and add them to another list without replacement

From Dev

Select a randomly number of items from a list and add them to another list (Swift)

From Dev

Get an item randomly from a list

From Dev

Prolog program - list of Peano numbers - rule returns multiple answers

From Dev

how to show a rule in prolog by passing a list with more items

From Dev

How to select an integer randomly from a list containing integers as well as other objects?

From Dev

How to select an integer randomly from a list containing integers as well as other objects?

From Dev

randomly select and execute a function from a list. Repeat the process and save the outputs

From Dev

List Read and Pass it to Right Clause Functor In Prolog

From Dev

Prolog Choose 3 From List

From Dev

Conversion from string to list in Prolog

From Dev

Angular 2 selecting option from select list based on passed in parameter

From Dev

R shiny pass variables from select list to reactive plot

Related Related

  1. 1

    Prolog: Select rule randomly from list and pass parameter

  2. 2

    How to randomly select an item from a list?

  3. 3

    randomly select a color from a premade list

  4. 4

    Select Randomly an object from a list (Image)

  5. 5

    Prolog beginner. How to take list as parameter and pass it on

  6. 6

    Is there a way to pass a compound term as a parameter from a form in swi-prolog

  7. 7

    Crawl iterating with a parameter over a list of urls from a rule with Scrapy

  8. 8

    How to select randomly from a list of string-array

  9. 9

    how to randomly select a certain number of elements from a list

  10. 10

    How do I randomly select a variable from a list, and then modify it in python?

  11. 11

    Python selenium: Randomly select from a url list textfile

  12. 12

    How can I select integers from a list in Prolog?

  13. 13

    how to pass a list in a predicate in prolog

  14. 14

    mod_rewrite rule not working (pass parameter from one url to another)

  15. 15

    Pass parameter from one select function to another using onchange event

  16. 16

    Rails - pass a parameter to controller from form select dropdown

  17. 17

    I want to randomly select items from a list and add them to another list without replacement

  18. 18

    Select a randomly number of items from a list and add them to another list (Swift)

  19. 19

    Get an item randomly from a list

  20. 20

    Prolog program - list of Peano numbers - rule returns multiple answers

  21. 21

    how to show a rule in prolog by passing a list with more items

  22. 22

    How to select an integer randomly from a list containing integers as well as other objects?

  23. 23

    How to select an integer randomly from a list containing integers as well as other objects?

  24. 24

    randomly select and execute a function from a list. Repeat the process and save the outputs

  25. 25

    List Read and Pass it to Right Clause Functor In Prolog

  26. 26

    Prolog Choose 3 From List

  27. 27

    Conversion from string to list in Prolog

  28. 28

    Angular 2 selecting option from select list based on passed in parameter

  29. 29

    R shiny pass variables from select list to reactive plot

HotTag

Archive