Prolog - 递归追加到列表返回 false [英] Prolog - Recursive append to list returning false

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

问题描述

标题说明了一切,但我们又来了.试图递归地附加到 Prolog 中的列表,虽然我之前已经通过临时缓冲区"让它工作了(通过 nb_setval/nb_getval)我想学习如何以稍微更合适的方式递归地附加到列表中.

Title says it all, but here we are again. Trying to append recursively to a list in Prolog, and while I have previously gotten it to work by having "temporary buffers" (via nb_setval/nb_getval) I'd like to learn how to, in a slightly more appropriate way, recursively append to lists.

我知道 Prolog 可以在所有绑定中工作,一旦绑定了一些东西就很难操作它,所以最初我接受了这个,但我明白为什么这不太奏效:

I've understood Prolog works all around bindings and once something is bound it's difficult to manipulate it, so initially I sat with this, but I've understood why that does not quite work:

recursiveAppend([], _).
recursiveAppend([H|T], Output):-
    append(H, [], Output),
    recursiveAppend(T, Output).

这让我更改了代码并转到以下内容:

That made me change the code and go to the following:

recursiveAppend([], _).
recursiveAppend([H|T], Output):-
    append(H, Output, NewOutput),
    recursiveAppend(T, NewOutput).

我希望这会奏效,因为它对我自己和其他人都有意义,同时也在搜索其他 StackOverflow 问题.不幸的是,在 SWI-Prolog 中调用这个谓词只会返回 false.

Which I had hoped would work, as it made sense to myself and apparently to others while scouring other StackOverflow questions as well. Unfortunately, calling this predicate in SWI-Prolog only returns false.

?- recursiveAppend([1, 2, 3, 4, 5], L1).假

在这种情况下,预期/期望的结果是:

Expected/desired result would, in this case, be:

?- recursiveAppend([1, 2, 3, 4, 5], L1).L1 = [1, 2, 3, 4, 5].

为了澄清起见,如果充实",程序的运行时应该是这样的:

For the sake of clarification, the runtime of the program should look something like this if "fleshed out":

recursiveAppend([H|T], Output):-
    % H is 1, Output is []
    append(H, Output, NewOutput),
    % NewOutput is [1]
    recursiveAppend(T, NewOutput).

recursiveAppend([H|T], Output):-
    % H is 2, Output is [1]
    append(H, Output, NewOutput),
    % NewOutput is [1, 2]
    recursiveAppend(T, NewOutput).

recursiveAppend([H|T], Output):-
    % H is 3, Output is [1, 2]
    append(H, Output, NewOutput),
    % NewOutput is [1, 2, 3]
    recursiveAppend(T, NewOutput).

recursiveAppend([H|T], Output):-
    % H is 4, Output is [1, 2, 3]
    append(H, Output, NewOutput),
    % NewOutput is [1, 2, 3, 4]
    recursiveAppend(T, NewOutput).

recursiveAppend([H|T], Output):-
    % H is 5, Output is [1, 2, 3, 4]
    append(H, Output, NewOutput),
    % NewOutput is [1, 2, 3, 4, 5]
    recursiveAppend(T, NewOutput).

recursiveAppend([], _). % First argument (list) is empty, and the second argument (list) has been populated (with [1, 2, 3, 4, 5]), program done.

感谢任何和所有帮助,即使这个问题之前可能被问过一百万次!

Any and all help is appreciated, even though this question has probably been asked a million times before!

推荐答案

Prolog 是一种不同的编程范式.它需要你忘记"您对编程的所有了解并以开放的心态学习.不要试图在使用普通"语言的同时学习 Prolog.变量并重新影响不同的值,Prolog 变量只有一个值或没有.它们可能仅在回溯时采用不同的值,并尝试为程序中的所有变量找到满足所有给定谓词的另一组值.

Prolog is a different programming paradigm. It requires you to "forget" all you know about programming and learn with an open mind. Don't try to learn Prolog while using "ordinary" variables and reaffecting different values, Prolog variables has only one value or none. They may take different values only on backtracking, and trying to find another set of values to all variables in your program that satisfies all the given predicates.

建议您阅读立即学习 Prolog"之类的书籍.互联网上可以免费获得许多州立大学的教程.

Suggest you to read books like "learn Prolog Now". Numerous tutorials from state universities are available free on the internet.

根据您最近编辑的 Calling recursiveAppend 示例,这里有一个符合示例的代码.

Based on your latest edit giving an example to Calling recursiveAppend, here's a code conform with the example.

recursiveAppend(X, Y) :- recursiveAppend(X, [], Y).

    recursiveAppend([], X, X).
    recursiveAppend([H|T], Current, Output):-
        append(Current, [H], NewTemp),
        recursiveAppend(T, NewTemp, Output).

您之前的代码返回 false,因为 append 需要列表作为参数.所以附加一个整数(输入列表的项目)总是会失败.我创建了一个版本 recursiveAppend/3 来累积第二个 arg 中的当前列表.在列表的末尾,当前列表成为最终输出.你会用更多的例子进一步测试它并告诉我们它是否按要求工作.

Your earlier codes returned false because append expects lists as arguments. So appending an integer (item of input list) will Always fail. I created a version recursiveAppend/3 to accumulate current list in the second arg. At the end of the list, the current list becomes the final output. Will you test it further with more examples and tell us if it is working as required.

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

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