Moving a variable in the break list

I go through the entry level in the prologue, making the problem of painting the map. Here is my code.

col(Colors,Map,Coloring) :-
    checkMap(Colors,Map,Coloring).
checkMap(Colors,[Country1:Country2],Coloring) :-
    goodColor(Country1:Country2,Coloring,Colors).
checkMap(Colors,[Country1:Country2|Rest],Coloring) :-
    goodColor(Country1:Country2,Coloring,Colors),
    checkMap(Colors,Rest,Coloring).
goodColor(Country1:Country2,Coloring,Colors) :-
    mem(Country1:Color1,Coloring),!,
    mem(Country2:Color2,Coloring),!,
    mem(Color1,Colors), mem(Color2,Colors),
    not(Color1=Color2).
mem(Var,[Var|_]).
mem(Var,[_|Rest]) :-
    mem(Var,Rest).

My conclusion is as follows:

?- col([a,b,c],[1:2,1:3,2:3],X).
X = [1:a, 2:b, 3:c|_G332] ;
X = [1:a, 2:c, 3:b|_G332] ;
X = [1:b, 2:a, 3:c|_G332] ;
X = [1:b, 2:c, 3:a|_G332] ;
X = [1:c, 2:a, 3:b|_G332] ;
X = [1:c, 2:b, 3:a|_G332] ;
fail.

Does anyone know how I can get rid of the final variable? I know that it is mostly cosmetic, but I don’t understand why it is there.

+3
source share
2 answers

Using an incomplete data structure is a valid Prolog programming method. If you intend to use an incomplete data structure, then one of them:

ground_terms([H|T1],[H|T2]) :- ground(H), !, ground_terms(T1,T2).
ground_terms(_,[]).

and change col as follows:

col(Colors,Map,Coloring) :-
    checkMap(Colors,Map,Coloring1),
    ground_terms(Coloring1,Coloring).
+1
source

The returned variable exists because it mem(Var,[Var|_])binds the unbound variable Coloringto [Var|_].

One way to avoid this is to accumulate the color of the card, for example (very fast and very dirty):

col(Colors,Map,Coloring) :-
    check(Colors,Map,[],Coloring).

check(Colors,[],Coloring,Coloring).

check(Colors,[Country1:Country2 | T],[],L) :-
    member(Color1,Colors),
    member(Color2,Colors),
    Color1 \== Color2,
    check(Colors,T,[Country1:Color1,Country2:Color2],L).

check(Colors,[Country1:Country2 | T],Coloring,L) :-
    member(Country1:Color1,Coloring),
    member(Country2:Color2,Coloring),!,
    check(Colors,T,Coloring,L).

check(Colors,[Country1:Country2 | T],Coloring,L) :-
    member(Country1:Color1,Coloring),!,
    member(Color2,Colors),
    not(member(_:Color2,Coloring)),
    check(Colors,T,[Country2:Color2|Coloring],L).

check(Colors,[Country1:Country2 | T],Coloring,L) :-
    member(Country2:Color2,Coloring),!,
    member(Color1,Colors),
    not(member(_:Color1,Coloring)),
    check(Colors,T,[Country1:Color1|Coloring],L).

Its a much more “procedural” approach than yours, though :-( Perhaps a more elegant way ...

0
source

Source: https://habr.com/ru/post/1699473/


All Articles