prolog - Remove item from multiple lists -
i have subproblem solution way larger suspect necessary.
the problem defined follows
remove x groups group has id between y , z or , b
expressed pseudo query looks y,z,a,b set 0,1,3,4
remove(x, [ period(0,1), period(3,4) ], [ group(0, [ subgroup([_,_,x,_,_]), subgroup([x])]), group(1, [ subgroup([x])]), group(2, [ subgroup([_,_,x])]), group(3, [ subgroup([_,x,_])]), group(4, [ subgroup([x,_,_])]) ], updatedgroups). the result be
updatedgroups = [ group(0, [ subgroup([_,_,_,_]), subgroup([])]), group(1, [ subgroup([])]), group(2, [ subgroup([_,_,x])]), group(3, [ subgroup([_,_])]), group(4, [ subgroup([_,_])]) ] so, solution is:
while start of current period less or equal end of current period, removal of x in groups, while "incrementing" start of day. repeat until no more periods
the removal of x in groups done "looping" groups , check if matches period, , if remove user subgroups, again done "looping".
this tedious straight forward solution, problem quite find myself doing stuff this, , cannot find approaches in less comprehensive way.
are there other approaches mine not cover 50+ lines?
updated
thanks lot, code became cleaner - might go further, possible post here (this modified bit - logic there)
inperiods(day, [ period(start,end) | _ ]) :- between(start,end, day). inperiods(day, [ _ | remainingperiods ]) :- inperiods(day, remainingperiods). rulegroupsinperiod(periods, rulegroup(day,_)) :- inperiods(day, periods). removeuserfromrelevantrulegroups(userid, periods, rulegroups, updatedrulegroups) :- include(rulegroupsinperiod(periods), rulegroups, includedrulegroups). exclude(rulegroupsinperiod(periods), rulegroups, excludedrulegroups), maplist(updaterulegroup(userid), includedrulegroups, updatedincludedrulegroups) append(updatedincludedrulegroups, excludedrulegroups, updatedrulegroups). updaterulegroup(userid, rulegroup(x, rulelist), rulegroup(x, updatedrulelist)) :- maplist(updaterule(userid), rulelist, updatedrulelist). updaterule(userid, rule(x, userlist), rule(x, updateduserlist)) :- delete(userlist, userid, updateduserlist).
yes.
the pattern describe common, , serious prolog systems ship powerful meta-predicates (i.e., predicates arguments denote predicates) let describe , many other common situations in flexible manner, using @ few simple additional definitions concrete relations.
richard o'keefe's proposal an elementary prolog library contains descriptions , implementations of many such predicates, becoming increasingly available in major prolog implementations , in prologue prolog.
in particular, should study:
include/3exclude/3maplist/[2,3]
note though many of described predicates impure in sense destroy declarative properties of code. in contrast true relations, won't able use them in all directions while preserving logical soundness.
exercise: of 3 predicates mentioned above, if any, preserve logical-purity when else pure, , which, if any, not?
Comments
Post a Comment