Prolog列表问题

发布于 2024-11-07 14:51:49 字数 218 浏览 2 评论 0原文

[a,b,c,d] and
[[1,2,3,4],[5,6,7,8],[43,34,56,5],[23,32,2,2]]

我想让

[[a,1,2,3,4],[b,5,6,7,8],[c,43,34,56,5],[d,23,32,2,2]]

我使用 swi prolog 可以吗?

多谢。

[a,b,c,d] and
[[1,2,3,4],[5,6,7,8],[43,34,56,5],[23,32,2,2]]

I want to make

[[a,1,2,3,4],[b,5,6,7,8],[c,43,34,56,5],[d,23,32,2,2]]

I use swi prolog is it possible do it ?

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

但可醉心 2024-11-14 14:51:50

SWI Prolog 可以使用两个短谓词来完成此操作:

merge0(A, B, Prev, Next) :- append(Prev, [[A|B]], Next).
merge(A, B, Result) :- foldl(merge0, A, B, [], Result).

以下是输入和输出的示例:

a(X) :- X = [a,b,c,d].
b(X) :- X = [[1,2,3,4],[5,6,7,8],[43,34,56,5],[23,32,2,2]].

?- a(A), b(B), merge(A, B, Result).

Result = [[a, 1, 2, 3, 4], [b, 5, 6, 7, 8], [c, 43, 34, 56, 5], [d, 23, 32, 2, 2]].

SWI Prolog can do this with two short predicates:

merge0(A, B, Prev, Next) :- append(Prev, [[A|B]], Next).
merge(A, B, Result) :- foldl(merge0, A, B, [], Result).

Here is example of input and output:

a(X) :- X = [a,b,c,d].
b(X) :- X = [[1,2,3,4],[5,6,7,8],[43,34,56,5],[23,32,2,2]].

?- a(A), b(B), merge(A, B, Result).

Result = [[a, 1, 2, 3, 4], [b, 5, 6, 7, 8], [c, 43, 34, 56, 5], [d, 23, 32, 2, 2]].
别忘他 2024-11-14 14:51:50

试试这个:

delete(X, [X|T], T).

delete(X, [Y|T], [Y|L]):-
delete(X, T, L).

insert(X, List, BigList):-
  delete(X, BigList, List).

if([],X,X).

if([H1|T1],[H2|T2],[SH|ST]):-
  insert(H1,H2,SH),!,
  if(T1,T2,ST).

我仔细检查过,它有效。

“if”代表“先插入”。

try this:

delete(X, [X|T], T).

delete(X, [Y|T], [Y|L]):-
delete(X, T, L).

insert(X, List, BigList):-
  delete(X, BigList, List).

if([],X,X).

if([H1|T1],[H2|T2],[SH|ST]):-
  insert(H1,H2,SH),!,
  if(T1,T2,ST).

I doubled checked, it works.

"if" stands for "insert first".

只为一人 2024-11-14 14:51:49
solve([], [], []).

solve([[X|Y]|S], [X|L1], [Y|L2]):-
  solve(S, L1, L2).

更新: 如何使用

将函数写在文件“a.pl”中,然后在 swi-prolog 中输入:

['a.pl'].

然后输入:

solve(X, [a,b,c,d], [[1,2,3,4],[5,6,7,8],[43,34,56,5],[23,32,2,2]]).

你会得到:

X = [[a, 1, 2, 3, 4], [b, 5, 6, 7, 8], [c, 43, 34, 56, 5], [d, 23, 32, 2, 2]] 

我有一种奇怪的感觉,我正在做你的作业。是吗?

solve([], [], []).

solve([[X|Y]|S], [X|L1], [Y|L2]):-
  solve(S, L1, L2).

UPDATE: How to use

Write the function in a file "a.pl", then in swi-prolog type:

['a.pl'].

then type:

solve(X, [a,b,c,d], [[1,2,3,4],[5,6,7,8],[43,34,56,5],[23,32,2,2]]).

You will get:

X = [[a, 1, 2, 3, 4], [b, 5, 6, 7, 8], [c, 43, 34, 56, 5], [d, 23, 32, 2, 2]] 

I have the strange feeling that I am doing your homework. Is it?

日裸衫吸 2024-11-14 14:51:49

使用 maplist/4Prolog lambda 像这样:

?- As  = [a,b,c,d], 
   Bss = [[1,2,3,4],[5,6,7,8],[43,34,56,5],[23,32,2,2]],
   maplist(\H^T^[H|T]^true,As,Bss,Css).
As  = [ a         , b         , c            , d           ],
Bss = [[  1,2,3,4],[  5,6,7,8],[  43,34,56,5],[  23,32,2,2]],
Css = [[a,1,2,3,4],[b,5,6,7,8],[c,43,34,56,5],[d,23,32,2,2]].

编辑

可以使用不同的 lambda 术语正如评论中指出的,在上面的 maplist/4 目标中。

  • maplist(\H^T^[H|T]^true,As,Bss,Css)
  • maplist(\H^T^ =( [H|T]) ,As,Bss,Css)

Use maplist/4 and Prolog lambdas like this:

?- As  = [a,b,c,d], 
   Bss = [[1,2,3,4],[5,6,7,8],[43,34,56,5],[23,32,2,2]],
   maplist(\H^T^[H|T]^true,As,Bss,Css).
As  = [ a         , b         , c            , d           ],
Bss = [[  1,2,3,4],[  5,6,7,8],[  43,34,56,5],[  23,32,2,2]],
Css = [[a,1,2,3,4],[b,5,6,7,8],[c,43,34,56,5],[d,23,32,2,2]].

Edit

Different lambda terms can be used in above maplist/4 goal, as pointed out in a comment.

  • maplist(\H^T^[H|T]^true,As,Bss,Css)
  • maplist(\H^T^ =([H|T]) ,As,Bss,Css)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文