在Prolog中屏蔽 [英] Masking in Prolog

查看:86
本文介绍了在Prolog中屏蔽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在尝试找出Prolog,并弄乱了Prolog中的列表列表.我正在尝试创建一种我认为在p中的面具 前言.我有一个谓词,它确定Prolog中两个列表列表之间的区别(L1和L2可以说),并将它们另存为列表列表(比如说R).我还有另一个谓词,它简单地指出差是否等于零(noDifference).我想基于L1和L2与R进行比较,得出两个结果列表列表(M1和M2).例如,如果某个位置为负值,我想将L1和L2的所有值与R进行比较然后,将L1相同位置的值保存到M1中.如果正值位于R的位置,则将L2相同位置的值保存到M2(如果有意义).在进行所有这些操作之前,我先使用noDifference函数检查差异是否为0,如果是,则M1和M2的列表列表的所有值均为0.

I have recently been trying to figure out Prolog and been messing with lists of lists in Prolog. I am trying to create a sort 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 a list(Lets say R). I have another predicate that simply states if the difference is equal to zero(noDifference). I would like to have two resulting lists of lists (M1 and M2) based off of L1 and L2 compared to the R. For example I would like to compare all values of L1 and L2 to R, if a negative value is at a location of R then the value in the same location of L1 is saved into M1. And if a positive value is at a location of R then the value in the same location of L2 is saved into M2 if that makes sense. Before all of this I check with my noDifference function to see if the difference is 0 and if so all values of M1 and M2's lists of lists will be 0.

这是我到目前为止所拥有的(我不确定我是否开始正确)

This is what I have so far(I'm not sure if I started it right)

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

其余的是一些示例值,看起来应该是这样

and for the rest of it here are what some example values should look like under 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 insight if what I am doing so far is right and how to properly formulate the subgoals/where I should go next would be awesome!

使用谓词进行编辑

?- 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]].

推荐答案

请考虑您的谓词应描述的内容.这是五个列表列表之间的关系,根据您提供的示例,这些列表具有相同的长度.这表明基本情况下有五个空列表.否则,所有五个列表的首部都是列表本身,它们之间具有特定的关系,我们称其为lists_mask_mlists/5.当然,对于尾巴也应如此,这可以通过递归目标来实现.因此您的谓词masker/5可能看起来像这样:

Think what your predicate should describe. It is a relation between five lists of lists which, according to the example you provided, are of same length. This suggests the base case with five empty lists. Otherwise the heads of all five lists are lists themselves, that are in a specific relation to each other, let's call it lists_mask_mlists/5. And of course the same should be true for the tails, which can be realized by a recursive goal. So your predicate masker/5 could look something like that:

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).

实际的掩蔽关系还具有一个包含五个空列表的基本情况.否则,还有另外两种情况:

The actual masking relation also has a base case with five empty lists. Otherwise there are two further cases:

1)当前掩蔽元素(第三列表的头)为负:第一列表的头是第四列表的头,第五列表的头是0

1) The current masking element (head of the third list) is negative: The head of the first list is the head of the fourth list and the head of the fifth list is 0

2)当前遮罩元素为正:第二个列表的头是第五个列表的头,第四个列表的头是0

2) The current masking element is positive: The head of the second list is the head of the fifth list and the head of the fourth list is 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).

使用此谓词,您的示例查询会产生所需的结果:

With this predicate your example query yields the desired result:

   ?- 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会产生实例化错误:

Note however, that due to < and >= this only works, if the third list is variable free. Replacing the first 4 in the third argument by a variable yields an instantiation error:

   ?- 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.包括该行

If you intend to use the predicate with a third argument that is not variable free, you might like to consider using clpfd. Include the line

:-use_module(library(clpfd)).

在您的源文件中,并像这样更改list_mask_mlists/5:

in your source file and alter lists_mask_mlists/5 like so:

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).

现在第二个查询也可以使用:

Now the second query works too:

   ?- 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

这篇关于在Prolog中屏蔽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆