list 如何在 Prolog 中附加列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11539203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:51:53  来源:igfitidea点击:

How do I append lists in Prolog?

listprologappendconcatenation

提问by Zik

How do I append lists in Prolog? I've searched on the Internet and I found this (from http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_7.html)

如何在 Prolog 中附加列表?我在互联网上搜索过,发现了这个(来自http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_7.html

append([X|Y],Z,[X|W]) :- append(Y,Z,W).  
append([],X,X).

So it gets the Zby removing the elements of [X|Y]in [X|W]. But how do I append two lists together?

所以它Z通过删除[X|Y]in的元素来获得[X|W]。但是如何将两个列表附加在一起?

Example,

例子,

appendlist([1,2],[3,4,5],X).

The result will be X = [1,2,3,4,5].

结果将是X = [1,2,3,4,5]

Also I don't know what happening in the recursion. (I traced it but didn't understand)

我也不知道递归中发生了什么。(我查了一下,但没看懂)

EDIT:What I want to know is how it should be coded to function like the predefined append()in Prolog.

编辑:我想知道的是它应该如何编码才能像append()Prolog 中预定义的那样运行。

回答by Will Ness

The code as you've posted it is (almost) OK. The order of clauses just needs to be swapped (in order to make this predicate definition productive, when used in a generative fashion):

您发布的代码(几乎)没问题。子句的顺序只需要交换(为了使这个谓词定义富有成效,当以生成方式使用时):

append( [], X, X).                                   % your 2nd line
append( [X | Y], Z, [X | W]) :- append( Y, Z, W).    % your first line

This defines a relationship between the three arguments, let's say A, Band C.

这定义了三个参数之间的关系,比如说ABC

Your first line says, " Cis the result of appending Aand Bif Aand Care non-empty lists, they both have the same head(i.e. first element), and the tailof Cis the result of appending the tail of Awith the same 2nd argument, B".

你的第一行说,C是附加的结果AB是否AC非空列表,它们都具有相同的头部(即第一个元素),和C是附加的尾巴的结果A与同第二个参数,B

  a        a
  ----------
  b        b
  c        c
  .    d   d
       e   e
       .   .

Or from left to right:

或者从左到右:

         a | b c .
           |     d e .
         a | b c d e .

append(         [], 
                 Z,
                 Z ).       
append( [X | Y   ],
                 Z,
        [X |         W ] ) :- append(
             Y,  Z,  W).

Think about it, it makes perfect sense. What it does is, we want to define the append/3relationship, and we know what we want it to be, so we just write down some obvious facts about it that we want it to fulfill, the laws that it must follow if you will.

仔细想想,这完全是有道理的。它的作用是,我们想要定义这种append/3关系,我们知道我们想要它成为什么,所以我们只需写下一些我们希望它实现的明显事实,如果你愿意,它必须遵守的法律。

So assuming we have this code already defined for us, what laws must it follow? Obviously, appending a tail of some list with another list gives us a tail of result of appending the full list with that 2nd list.

那么假设我们已经为我们定义了这个代码,它必须遵循哪些法律?显然,将某个列表的尾部附加到另一个列表中会给我们一个将完整列表与第二个列表附加在一起的结果的尾部。

This defines how we "slide along" the first list. But what if there's nowhere more to slide? What if we've reached the end of that list? Then we've arrived at the empty list, and appending an empty list with another list gives us that list as the result. Obviously. And that's what that 2nd line in your code is telling us, it says, "appending an empty listwith another list produces that list as the result".

这定义了我们如何“沿着”第一个列表“滑动”。但是如果没有其他地方可以滑动怎么办?如果我们已经到达该列表的末尾怎么办?然后我们到达了空列表,将另一个列表附加到一个空列表中,我们得到了该列表作为结果。明显地。这就是你的代码中的第二行告诉我们的,它说,“将一个空列表与另一个列表相加会产生该列表作为结果”

Amazingly, having written down these two laws that append/3must follow, is the same as writing down the definition itself.

令人惊讶的是,写下这两条append/3必须遵循的定律,就等于写下了定义本身。

addition:this explains it from a declarative point of view; do check out an answer by m09which shows it more from the operational point of view.

另外:这是从声明的角度解释的;请查看m09 的答案,它从操作的角度更多地展示了它。

回答by aioobe

But how do I append two lists together?

但是如何将两个列表附加在一起?

You answered your own question: You use append/3.

您回答了自己的问题:您使用append/3.

If you want to append Xand Yand store the result in Z, you do

如果你想追加XY存储结果Z,你可以

append(X, Y, Z)

If for example X = [1, 2]and Y = [3, 4, 5]then Zwill be bound to [1, 2, 3, 4, 5]:

例如X = [1, 2],如果Y = [3, 4, 5]然后Z将绑定到[1, 2, 3, 4, 5]

| ?- append([1,2],[3,4,5], X).

X = [1,2,3,4,5]

yes
| ?-