从 Prolog 递归函数中获取输出值 [英] Get the output value from a Prolog recursive function

查看:36
本文介绍了从 Prolog 递归函数中获取输出值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些天我正在学习 Prolog,需要将两个列表合并为一个遵循此规则的列表:

I am learning Prolog these days and need to combine two list into one that follows this rule:

ListA: [], ListB: [] => ResultList: []
ListA: [], ListB:[a,b,c] => ResultList: [[a],[b],[c]]
ListA: [1,2], ListB:[a,b,c] => ResultList: [[1,2,a],[1,2,b],[1,2,c]]

我遇到了递归函数输出值问题,这是我的编码:

I am stuck with a recursive function output value problem and here is my coding:

extend_my_path([],_,_,_).
extend_my_path([H|T],OriginalPath,OrignailMewPaths,NewPaths) :-
    append(OriginalPath,[H],NewPath),
    append(OrignailMewPaths,[NewPath],NewPaths1),
    print("NewPaths1:"),print(NewPaths1), nl,
    extend_my_path(T,OriginalPath,NewPaths1,NewPaths1).

运行它会给出以下输出,没有 Result 变量的值:

Running it gives the following output without the value of Result varable:

?- extend_my_path([a,b,c],[1,2],[],Result).
"NewPaths1:"[[1,2,a]]
"NewPaths1:"[[1,2,a],[1,2,b]]
"NewPaths1:"[[1,2,a],[1,2,b],[1,2,c]]
true.

我希望得到它的价值:

Result=[[1,2,a],[1,2,b],[1,2,c]]

如果有人能指出原因,我们将不胜感激.谢谢.

It is highly appreciated if anyone can point out the reason. Thanks.

推荐答案

我在这六行代码中看到了相当多的混乱.以下是一些错误的线索:

I see quite a bit of confusion in these six lines of code. Here are some clues that something is amiss:

  • 您的问题是根据三件事定义的,但您的谓词有四个参数.最后两个应该是什么?
  • 你有一个单例变量 NewPaths 这是一场灾难
  • 您的基本案例将一个空列表与任何其他三件事相关联!试试看:extend_my_path([], Lightning, [the,sound,of,rain], "fresh coffee") 是真的!这似乎不是您想要的.
  • 有人想知道Orignail Mew Paths"是什么,或者paths"与这里的任何事情有什么关系.错别字很重要,请改正!
  • 归纳案例以 NewPaths1 的两个副本结束,这看起来很可疑,因为我们在开始时有两个非常不同的变量,正如您在基本案例中看到的那样,它们是什么并不重要.奇怪!
  • Your problem is defined in terms of three things, but your predicate has four arguments. What are the last two supposed to be?
  • You have a singleton variable NewPaths which is a disaster
  • Your base case relates an empty list to any three other things! Try it: extend_my_path([], lightning, [the,sound,of,rain], "fresh coffee") is true! This seems unlikely to be what you intended.
  • One wonders what an "Orignail Mew Paths" is, or what "paths" have to do with anything here. Typos matter, fix them!
  • The inductive case ends with two copies of NewPaths1, which seems suspicious because we had two very different variables at the start, and as you see in the base case it doesn't matter what they are. Odd!

我认为这里发生的事情是您认为将内容添加到列表的唯一正确方法是使用 append/3,但事实并非如此.我认为进一步的 Prolog 的单赋值语义使您相信您需要另一个参数来创建空列表供您附加.你错过了递归的基本情况,所以你永远不会得到合理的回报;让它大开至少让你相信但没有给你绑定.这里的根本问题是您的代码中没有合理的关系.让我们再试一次.

I think what happened here is that you figured the only and right way to add things to a list is with append/3, which isn't true. I think further Prolog's single-assignment semantics convinced you that you needed another argument to create the empty list for you to append to. You missed the base case for your recursion so you never got a sensible return; making it wide open at least got you to true but didn't give you a binding. And the fundamental problem here is that there isn't a sensible relation in your code. Let's try again.

您正在尝试编写一个名称和参数更像这样的谓词:

You're trying to write a predicate whose name and arguments are something more like this:

% extend_path(Path, Possibilities, NewPaths)

这里不需要第四个参数,因为你不需要知道到目前为止你在创建 NewPaths 时做了什么,你可以只为当前的可能性构建路径并知道递归正在处理休息.

There's no need for a fourth argument here, because you don't need to know what you've done so far in creating NewPaths, you can just build the paths for the current possibility and know that recursion is taking care of the rest.

你的基本情况是你没有更多的可能性,因此没有新的路径:

Your base case would then be the case where you have no more possibilities, and thus, no new paths:

extend_path(_, [], []).

这并没有说明任何事情与任何事情有关,而是说任何前缀的路径扩展,当我没有可能时,都是空的."

This doesn't say anything-relates-to-anything, it says "the path extensions of any prefix, when I'm out of possibilities, is empty."

您的归纳案例然后采用您的第一个参数并将其附加到您的可能性列表中的下一项.这在您的新路径列表中.

Your inductive case then takes your first argument and appends it onto the next item from your list of possibilities. This goes on your list of new paths.

extend_path(Prefix, [Ext1|ExtRest], [Next1|NextRest]) :-
    append(Prefix, [Ext1], Next1),
    extend_path(Prefix, ExtRest, NextRest).

这字面意思是给定一些前缀,并且我的扩展列表中还有一个 Ext,将生成一个新的路径扩展 Next1,例如:Prefix + [Ext1] 是 Next1."实际上,它说的比递归步骤要多一些.但是这里的想法是咬掉一个片段,Ext1,然后将它与一个结果片段 Next1 匹配,然后将其余的输入与其余的输出.

This literally says "given some Prefix, and that I have an Ext left in my list of extensions, will produce a new path extension Next1, such that: Prefix + [Ext1] is Next1." Actually, it says a little more than that with the recursive step. But the idea here is bite off one piece, Ext1, and match it up with one piece of the result, Next1, and then match up the rest of the input with the rest of the output.

最后,不要将单例变量警告"视为警告.这不是,就像 Python 会因为函数之间没有足够的换行符而生气.单例变量几乎总是严重错误,特别是对于 Prolog 的新用户!关注他们!

Lastly, please do not treat a singleton variable "warning" as a warning. This is not like Python being mad that you don't have enough newlines between functions. Singleton variables are almost always CRITICAL ERRORS, especially for new users of Prolog! Pay attention to them!

希望这会有所帮助!

这篇关于从 Prolog 递归函数中获取输出值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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