我如何在追加序言名单? [英] How do I append lists in prolog?

查看:121
本文介绍了我如何在追加序言名单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何追加列出了序言?我搜索在互联网上,我发现这(从<一个href=\"http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_7.html\">http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_7.html)

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

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

因此​​它的以Z 通过去除元素|在[X Y] [X | W] 。但我怎么两个列表追加在一起吗?

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

例如:

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

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

此外,我不知道在什么样的递归发生。 (我追查,但不明白)

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

编辑:我想知道的是应该如何codeD的功能就像在序言中的predefined追加()

What I want to know is how it should be coded to function like the predefined append() in prolog

推荐答案

当你已经张贴在该code是(几乎)确定。条款的顺序只需要被交换(为了使这个predicate定义富有成效,在生成的方式使用时):

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

这定义了三个参数之间的关系,比方说 A B ç

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

您第一行说, C 是附加的结果 A B 如果 A C 都是非空列表,它们都具有同样的标题(即第一个元素),并在 的C 是附加<$尾部的结果C $ C> A 用相同的第二个参数, B

Your first line says, " C is the result of appending A and B if A and C are non-empty lists, they both have the same head (i.e. first element), and the tail of C is the result of appending the tail of A with the same 2nd argument, B".

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

仔细想想,它非常有意义。它所做的是,我们要定义的添加/ 3 的关系,我们知道我们希望它是,所以我们只是写下关于它的一些显而易见的事实,我们希望它履行,法律,它必须遵循,如果你会的。

Think about it, it makes perfect sense. What it does is, we want to define the append/3 relationship, 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.

所以,假设我们已经为我们定义了这个code,有什么法律必须跟随它?很明显,一些追加列表的尾部与另一个列表让我们与第二清单追加的完整列表的结果的尾巴。

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.

这定义了我们的第一个列表一起幻灯片。但是,如果有无处更下滑?如果我们已经达到了列表的末尾?然后,我们来到了空列表,并追加空单与另一个列表为我们提供了列表作为结果。明显。这就是在你的code的第2行告诉我们,它说,追加一个空列表的另一个列表生成一个列表作为结果

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 list with another list produces that list as the result".

令人惊讶的,有写下来这两部法律的添加/ 3 必须遵循,是一样写下定义本身。

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

此外:的这解释了它从一个声明点;做检查出由M09 一个答案,显示更从操作上来看。

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

这篇关于我如何在追加序言名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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