Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List Prolog中的乘法列表和列表列表_List_Prolog_Matrix Multiplication - Fatal编程技术网

List Prolog中的乘法列表和列表列表

List Prolog中的乘法列表和列表列表,list,prolog,matrix-multiplication,List,Prolog,Matrix Multiplication,我目前正在编写一个Prolog程序,在弄清楚如何实现它时遇到了很多困难 我想把像[1,2,2,1]这样的列表放入一个函数中。通过这个例子,我想让它自身相乘,生成一个新的矩阵 1 * [1,2,2,1] which would yield [1,2,2,1] 2 * [1,2,2,1] which would yield [2,4,4,2] 2 * [1,2,2,1] which would yield [2,4,4,2] 1 * [1,2,2,1] which would yield [1,2,

我目前正在编写一个Prolog程序,在弄清楚如何实现它时遇到了很多困难

我想把像[1,2,2,1]这样的列表放入一个函数中。通过这个例子,我想让它自身相乘,生成一个新的矩阵

1 * [1,2,2,1] which would yield [1,2,2,1]
2 * [1,2,2,1] which would yield [2,4,4,2]
2 * [1,2,2,1] which would yield [2,4,4,2]
1 * [1,2,2,1] which would yield [1,2,2,1]
我希望它能将所有这些元素组合成一个矩阵,如:

[[1,2,2,1],[2,4,4,2],[2,4,4,2],[1,2,2,1]].
最后一部分是,当我自己相乘时,我想把它归零。因此,第二个点将使第二个点归零,形成最终矩阵:

[[0,2,2,1],[2,0,4,2],[2,4,0,2],[1,2,2,0]].
我想有一个谓词,它调用另一个谓词,从而生成每个列表。我的想法如下:

main(A,O):-
    second(A,A,O).

second([],_,[]).
second([A|As],B,[O|Os]):- %creates the list of lists.
    third(A,B,O),
    second(As,B,Os).

third(_,[],[]).
third(A,[B|Bs],[O|Os]):-
    fourth(A,B,O),
    third(A,Bs,Os). %multiplies single digit by list.
fourth(A,B,0):- A == B.
fourth(A,B,O):- O is A * B.
我得到了正确的矩阵,但不能得到零对角线


我只是想不出一个正确的方法来得到对角线上有零的矩阵。有什么想法吗?

您可以通过引入指示您所在行和列的索引并检查是否匹配来实现零:

main(A, O) :-
    second(A, A, 0, O).

second([], _, _, []).
second([A|As], B, R, [O|Os]) :- %creates the list of lists.
    third(A, B, 0, R, O),
    R1 is R + 1,
    second(As, B, R1, Os).

third(_, [], _, _, []).
third(A, [B|Bs], C, R, [O|Os]) :-
    fourth(A, B, C, R, O),
    C1 is C + 1,
    third(A, Bs, C1, R, Os). %multiplies single digit by list.

fourth(_, _, X, X, 0).
fourth(A, B, C, R, O) :- C \== R, O is A * B.
检查:

| ?-  main([1,2,2,1], L).

L = [[0,2,2,1],[2,0,4,2],[2,4,0,2],[1,2,2,0]] ? ;

no

另一个有趣的方法是创建一个带有索引的
maplist\u
谓词,其工作原理与
maplist
类似,但管理索引并隐式假设给定谓词接受索引作为其第一个参数:

maplist_with_index(Pred, L, M) :-
    maplist_with_index_(Pred, 0, L, M).
maplist_with_index_(Pred, I, [H|T], [M|Ms]) :-
    Pred =.. [P|Pt],
    append([P,I|Pt], [H], NewPred),
    Call =.. NewPred,
    call(Call, M),
    I1 is I + 1,
    maplist_with_index_(Pred, I1, T, Ms).
maplist_with_index_(_, _, [], []).
然后,使用该谓词的矩阵程序如下所示:

main(A, O) :-
    second(A, A, O).

second(M, A, O) :-
    maplist_with_index(third(A), M, O).

third(R, A, E, O) :-
    maplist_with_index(fourth(R, E), A, O).

fourth(X, X, _, _, 0).
fourth(C, R, A, B, O) :- C \== R, O is A * B.

您是否尝试过跟踪查询
main
second
?查看
second
谓词的第二个参数(变量
B
)。在递归调用中,
B
永远不会“减少”,但基本情况要求它减少到
[]
。因此,
second
将失败。是的,它在第二秒内失败的部分是失败的:(10)second([],[1,2,2,1],_G5307)。那么它将是uu而不是[]?太好了!我得到了正确的矩阵,但不是。我需要计算出零部分。
0
之所以存在,是因为您的基本情况提供了
0
作为列表的最后一个尾部。换句话说,您在以前用于列表的参数中使用了
0
。我的意思是找出如何使最终的矩阵具有0我在上面解释的点。哇,这一切都是有道理的。如果我想把一个列表放到主列表中,我想把结果列表加在一起,创建一个单一的列表。这可以做到吗?我假设我必须做一个状态的头尾?