Disguise in Prolog

I recently tried to figure out Prolog and mess with list listings in Prolog. I am trying to create a kind of mask, I suppose, in p Prolog. I have a predicate that determines the difference between two lists of lists (L1 and L2 lets say) in Prolog and saves them as a list of lists (Lets say R). I have another predicate that simply indicates whether the difference is zero (noDifference). I would like to have two resulting list lists (M1 and M2) based on L1 and L2, compared to R. For example, I would like to compare all the values ​​of L1 and L2 with R, if the negative value is in a location from R, then the value at the same location, L1 is stored in M1. And if the positive value is in location R, then the value in the same place L2 is stored in M2, if that makes sense. First of all, I will check with the noDifference function,to see if the difference is 0, and if all the values ​​of the lists of the lists of the lists M1 and M2 are 0.

This is what I still have (I'm not sure if I started correctly)

masker(L1,L2,R,M1,M2):- noDifference(R1), M1=R, M2=R1;

and for the rest, here are the meanings of some examples in the hood

L1=[[1,5,3,8],[1,5,3,8]]
L2=[[5,4,7,4],[5,4,7,4]]
R=[[4,-1,4,-4],[4,-1,4,-4]]
M1=[[0,5,0,8],[0,5,0,8]]Neg values of L1 at R are stored rest are 0)
M2=[[5,0,7,0],[5,0,7,0]](Pos values of L2 at R are stored rest are 0)

Any understanding of what I am doing so far, correctly and how to correctly formulate subgoals, where I should go next, would be wonderful!

change using predicate ex

?- masker([[1,5,3,8],[1,5,3,8]],
          [[5,4,7,4],[5,4,7,4]],
          [[4,-1,4,-4],[4,-1,4,-4]], M1, M2).
M1=[[0,5,0,8],[0,5,0,8]].
M2=[[5,0,7,0],[5,0,7,0]].
+3
source share
2 answers

, . , , , . . , , list_mask_mlists/5. , , , . , -/5 :

masker([],[],[],[],[]).
masker([X|Xs],[Y|Ys],[M|Ms],[R1|R1s],[R2|R2s]) :-
   lists_mask_mlists(X,Y,M,R1,R2),
   masker(Xs,Ys,Ms,R1s,R2s).

. :

1) ( ) : , - 0

2) : , 0

:

lists_mask_mlists([],[],[],[],[]).
lists_mask_mlists([X|Xs],[_Y|Ys],[M|Ms],[X|R1s],[0|R2s]) :-   % 1)
   M < 0,
   lists_mask_mlists(Xs,Ys,Ms,R1s,R2s).
lists_mask_mlists([_X|Xs],[Y|Ys],[M|Ms],[0|R1s],[Y|R2s]) :-   % 2)
   M >= 0,
   lists_mask_mlists(Xs,Ys,Ms,R1s,R2s).

:

   ?- masker([[1,5,3,8],[1,5,3,8]],[[5,4,7,4],[5,4,7,4]],[[4,-1,4,-4],[4,-1,4,-4]],M1,M2).
M1 = [[0,5,0,8],[0,5,0,8]],
M2 = [[5,0,7,0],[5,0,7,0]] ? ;
no

, , - < >= , . 4 :

   ?- masker([[1,5,3,8],[1,5,3,8]],[[5,4,7,4],[5,4,7,4]],[[X,-1,4,-4],[4,-1,4,-4]],M1,M2).
     ERROR at  clause 2 of user:masked/5 !!
     INSTANTIATION ERROR- =:=/2: expected bound value

, , clpfd.

:-use_module(library(clpfd)).

list_mask_mlists/5 :

lists_mask_mlists([],[],[],[],[]).
lists_mask_mlists([X|Xs],[_Y|Ys],[M|Ms],[X|R1s],[0|R2s]) :-
   M #< 0,                                                    % <- here
   lists_mask_mlists(Xs,Ys,Ms,R1s,R2s).
lists_mask_mlists([_X|Xs],[Y|Ys],[M|Ms],[0|R1s],[Y|R2s]) :-
   M #>= 0,                                                   % <- here
   lists_mask_mlists(Xs,Ys,Ms,R1s,R2s).

:

   ?- masker([[1,5,3,8],[1,5,3,8]],[[5,4,7,4],[5,4,7,4]],[[X,-1,4,-4],[4,-1,4,-4]],M1,M2).
M1 = [[1,5,0,8],[0,5,0,8]],
M2 = [[0,0,7,0],[5,0,7,0]],
X in inf.. -1 ? ;
M1 = [[0,5,0,8],[0,5,0,8]],
M2 = [[5,0,7,0],[5,0,7,0]],
X in 0..sup ? ;
no
+5

@tas (+1).

, . CLP (FD):

?- masker([[1,5,3,8],[1,5,3,8]],[[5,4,7,4],[5,4,7,4]],[[4,-1,4,-4],[4,-1,4,-4]],M1,M2).
M1 = [[0, 5, 0, 8], [0, 5, 0, 8]],
M2 = [[5, 0, 7, 0], [5, 0, 7, 0]] ;
false.

; false , , Prolog , . p >

, , , .

, , .

if_/3.

, if_/3, - CLP (FD) & tbsp; (#<)/2, zcompare/3:

#<(X, Y, T) :-
        zcompare(C, X, Y),
        less_true(C, T).

less_true(<, true).
less_true(>, false).
less_true(=, false).

:

:- use_module(library(clpfd)).

masker([], [], [], [], []).
masker([X|Xs], [Y|Ys], [M|Ms], [R1|R1s], [R2|R2s]) :-
        lists_mask_mlists(X, Y, M, R1, R2),
        masker(Xs, Ys, Ms, R1s, R2s).

lists_mask_mlists([], [], [], [], []).
lists_mask_mlists([X|Xs], [Y|Ys], [M|Ms], R1s0, R2s0) :-
        if_(M #< 0,
            (R1s0 = [X|R1s], R2s0 = [0|R2s]),
            (R1s0 = [0|R1s], R2s0 = [Y|R2s])),
        lists_mask_mlists(Xs, Ys, Ms, R1s, R2s).

:

?- masker([[1,5,3,8],[1,5,3,8]],[[5,4,7,4],[5,4,7,4]],[[4,-1,4,-4],[4,-1,4,-4]],M1,M2).
M1 = [[0, 5, 0, 8], [0, 5, 0, 8]],
M2 = [[5, 0, 7, 0], [5, 0, 7, 0]].

!

- , !

+4

All Articles