添加到 Prolog 中的列表列表 [英] Adding to a list of lists in Prolog

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

问题描述

我目前正在尝试编写一个 Prolog 程序,该程序会将给定的字符添加到列表的末尾.我要附加的列表是列表中的元素.这是我目前拥有的.

I am currently attempting to write a Prolog program which will add a given character to the end of a list. The list's I want to append are elements within a list. This is what I currently have.

extends(X, [], []).
extends(X, [[Head]|Lists], Y):-
    append([X], [Head], Y),
    extends(X, Lists, [Y]).

这里我试图连接 X 和 Head,将其存储在 Y 中.但是我希望 Y 是一个列表列表,因此当它再次重复该过程时,下一个连接也将存储在 Y 中.所以在程序 Y 的结尾将存储所有连接的结果.我希望结果如下所示.

Here I'm attempting to concatenate X and Head, storing it in Y. However I want Y to be a list of lists, so when it repeats the process again the next concatenation will be stored also in Y. So at the end of the program Y would store the results of all the concatenations. I would want the result to look like as follows.

?- extends(a, [[b,c], [d,e,f], [x,y,z]], Y).
Y = [[b,c,a], [d,e,f,a], [x,y,z,a]].

有人能帮我解决这个问题吗?

Could anyone help me out with this?

推荐答案

你想对两个列表的对应元素应用一些操作.该操作涉及列表本身.很容易与列表的嵌套级别混淆,所以让我们尝试不要考虑这些术语.相反,首先定义一个对 one 列表进行扩展的谓词:

You want to apply some operation to corresponding elements of two lists. That operation talks about lists itself. It's easy to get confused with the nested levels of lists, so let's try not to think in those terms. Instead, define first a predicate that does the extension of one list:

element_list_extended(Element, List, Extended) :-
    append(List, [Element], Extended).

使用您示例中的案例,其行为如下:

This behaves as follows, using cases from your example:

?- element_list_extended(a, [b, c], Extended).
Extended = [b, c, a].

?- element_list_extended(a, List, [x, y, z, a]).
List = [x, y, z] ;
false.

目前看起来不错.我们需要做的就是将这个操作应用到两个列表的对应元素上:

Looks good so far. All we need to do is to apply this operation to corresponding elements of two lists:

extends(_Element, [], []).
extends(Element, [Xs | Xss], [Ys | Yss]) :-
    element_list_extended(Element, Xs, Ys),
    extends(Element, Xss, Yss).

这有效:

?- extends(a, [[b,c], [d,e,f], [x,y,z]], Y).
Y = [[b, c, a], [d, e, f, a], [x, y, z, a]] ;
false.

使其发挥作用的关键是将问题分解为两个部分,并分别解决这些更简单的部分.

The key to making it work was to decompose the problem into two parts and to solve those simpler parts separately.

现在,如果我们愿意,由于 element_list_extended/3 的定义是包含单个目标的单个子句,我们可能决定不使用它并将其定义内联到 extends/3:

Now, if we like, since the definition of element_list_extended/3 is a single clause containing a single goal, we might decide to do without it and inline its definition into extends/3:

extends(_Element, [], []).
extends(Element, [Xs | Xss], [Ys | Yss]) :-
    append(Xs, [Element], Ys),
    extends(Element, Xss, Yss).

如您所见,您已经非常接近了!您只是有一些多余的括号,因为您对列表嵌套感到困惑.这正是分解问题的帮助所在.

As you can see, you were quite close! You just had some superfluous brackets because you got confused about list nesting. That's precisely where decomposing the problem helps.

(正如另一个答案所说,SWI-Prolog 有一些有用的库,可以让您用更短的代码来表达这一点.)

(As the other answer said, SWI-Prolog has some useful libraries that allow you to express even this in even shorter code.)

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

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