Execute a predicate stored in a clause (or fact)
Posted: 7 Jul 2022 21:24
I was thinking about ways to reduce code complexity in some menu functions and I seem to recall that it might be possible to store menu actions in a clause (or fact) structure, something like this:
Is something like this possible?
Code: Select all
class predicates doAction : (integer SelectionIndex, predicate ExecuteSelection [out]) determ. clauses doAction(1, one). doAction(2, two). doAction(_, oops). class predicates % to be executed, dependent upon the input to doAction/2 one : (). two : (). oops : (). clauses one() :- stdio::write("\n", 1). two() :- stdio::write("\n", 2). oops() :- stdio::write("\nOOPS!"). clauses run() :- N = 1, % index of a menu selection doAction(N, Pred), nothing(Pred), % What to do to execute 'Pred' here ??? !. run() :- nothing("FAILED").