Build compound predicate from list in Prolog -
this question has answer here:
- list of predicates in prolog 1 answer
if have list of predicates in prolog [flies, swims]
, how can build predicate conjunction of predicates in list, ie fliesandswims(x) :- flies(x), swims(x).
?
alternatively, there better way of building predicate @ runtime without putting component predicates in list , building compound 1 them when needed?
edit: turns out duplicate of list of predicates in prolog. had found answer, thought returned whether given atom matched every predicate in list. didn't realise can pass variable instead of atom , have return every case matches well.
library(lambda) powerful, has cost. if think 'simpler better' (wrt debugging, specially...) consider
call_unary_list([], _). call_unary_list([p|ps], x) :- call(p, x), call_unary_list(ps, x).
let's compare performances:
compare_call_list :- findall(flies, between(1,100000,_), l), time(call_unary_list(l, _)), time(maplist(call_unary(_), l)), time(maplist(x+\pred^call(pred,x), l)). call_unary(x, p) :- call(p, x). ?- compare_call_list. % 200,000 inferences, 0.123 cpu in 0.123 seconds (100% cpu, 1629657 lips) % 300,000 inferences, 0.145 cpu in 0.149 seconds (98% cpu, 2064184 lips) % 1,000,001 inferences, 1.286 cpu in 1.297 seconds (99% cpu, 777362 lips) true .
the call_unary/2 highlights arguments swap that's required maplist meta predicate
Comments
Post a Comment