如何在 Prolog 中附加列表? [英] How do I append lists in Prolog?

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

问题描述

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

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)

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

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

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)

我想知道的是它应该如何编码为像 Prolog 中预定义的 append() 一样的功能.

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

推荐答案

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

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 *) 

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

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

你的第一行说,"C 是附加 AB 的结果,如果 AC 是非-空列表,它们都有相同的head(即第一个元素),Ctail是附加的尾部的结果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
       .   .

或者从左到右:

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

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

仔细想想,很有道理.它的作用是,我们想要定义 append/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.

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

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

令人惊奇的是,写下append/3 必须遵循的这两条定律,与写下定义本身是一样的.

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

addition: 这是从声明的角度解释的;请查看 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.

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

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