Check the list of items and requirements in the prologue
I have a problem with Prolog. If it were another language with an imperative paradigm, it would be easier for me, but this is not the main thing. There are several points, some of which have requirements that must be obtained. For example, if I have element b, I already have element a.
to_get(b):-
need(a).
to_get(a3):-
need(a2),
need(a1).
So, I have a list with items. I have to check what elements are there and then check if all the requirements for the element are there. If not, then I need to create a new list and add it there. And bring back this new list.
L1 = [b, a1, a3]
b requires a, but a is not in L1, so means it is in a new list, L2
L2 = [a, a2]
I hope this is not such a stupid question, I'm just new to the prologue. Thanks to
source to share
You are probably assuming that there is a relationship to describe dependencies between items. Below is used
closure0/3
and non_member/2
.
item_needs(b, a). item_needs(a3, a2). item_needs(a3, a1). items_missing(Items, Needed) :- member(Item, Items), closure0(item_needs, Item, Needed), non_member(Needed, Items). ?- items_missing([b,a1,a3],Missing). Missing = a ; Missing = a2 ; false.
To get this listed use setof/3
:
?- setof(M,items_missing([b,a1,a3],M),Ms). Ms = [a, a2].
And in general:
items_missingitems(Items, Ms) :-
( ground(Items) -> true
; throw(error(instantiation_error,_)) ), % safety check
( length(Items,_) -> true
; throw(error(type_error(list,Items),_)) ), % one more
setof(M, items_missing(Items,M), Ms).
source to share