Prolog - 用事实递归 [英] Prolog - Recursive with facts

查看:48
本文介绍了Prolog - 用事实递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想提一下,这个问题是在以前的考试中提出的,但我不太明白答案.我只需要有人来照亮.

Firstly, I want to mention that this question was asked in a previous exam but I don't quite understand the answer. I just need someone to shed some light.

代码描述如下:

state( 1, f, 2 ).
state( 2, o, 3 ).
state( 2, l, 4 ).
state( 3, n, 5 ).
state( 3, r, 6 ).
state( 4, o, 7 ).
state( 5, d, 8 ).
state( 6, t, 9 ).
state( 6, m, 10 ).
state( 7, u, 11 ).
state( 8, u, 12 ).
state( 10, e, 13 ).

find_( Y1, [ L | ES ], X ) :-
    state( Y1, L, Y2 ),
    find_( Y2, ES, X ).
find_( Y, [], Y ).

假设我们调用 find_(1, Z, 9) 答案将是 Z = [f,o,r,t].

Let's say we call find_(1, Z, 9) the answer will be Z = [f,o,r,t].

在递归调用之前,它是否会统一 Y2 的所有可能值?如果是这样,为什么答案不包括 state(2, l, 4) 的第二个统一中的 l.

Before the recursive call, is it going to unify with all possible value for Y2 ? If so, why the answer is not including the l from the second unification for state( 2, l, 4).

我尝试过跟踪模式,但这对我没有多大帮助.

I've tried the trace mode, but that didn't help me much.

谢谢.

推荐答案

如果您将状态事实绘制为节点和边的图形.

If you draw the state facts as a graph of nodes and edges you get.

现在查询要求从 1 到 9 的路径,结果是从 1 到 9 的边列表.它们是绿色的.边是 f,o,r,t.

Now the query ask for a path from 1 to 9 and the result is a list of the edges from 1 to 9. They are colored green. The edges are f,o,r,t.

有时试图以 Prolog 的形式理解问题比试图以不同的方式理解问题更难.正如我经常注意到的,当遇到困难时,一个通常有帮助的选项是拿出笔和纸.

Sometimes trying to understand the problem as Prolog is harder than trying to understand the problem in a different manner. As I often note when stuck one option that often helps is to get out a pen and paper.

在递归调用之前,是不是要统一所有可能的Y2 的值?

Before the recursive call, is it going to unify with all possible value for Y2 ?

没有.Prolog 按照它们的写入顺序统一谓词/事实,然后在失败时回溯到下一个.所以 state(2,o,3) 首先尝试并成功打印答案.然后因为 state(2,_,_) 有一个选择点,然后选择了 state(2,l,4).然而,这最终失败了,因此不作为单独的答案包含在内.

No. Prolog unifies the predicates/facts in the order they are written and then backtracks to the next one upon failure. So state(2,o,3) is tried first and succeeds for which the answer is printed. Then because there is a choice point for state(2,_,_) for which state(2,l,4) is then selected. However this eventually fails and thus is not included as a separate answer.

TL;博士

我使用 graphviz dot 与文件

so_question_07.gv

so_question_07.gv

digraph so_question_07 {


    node_01 [label="1", color="green", fontcolor="green"];
    node_02 [label="2", color="green", fontcolor="green"];
    node_03 [label="3", color="green", fontcolor="green"];
    node_04 [label="4"];
    node_05 [label="5"];
    node_06 [label="6", color="green", fontcolor="green"];
    node_07 [label="7"];
    node_08 [label="8"];
    node_09 [label="9", color="green", fontcolor="green"];
    node_10 [label="10"];
    node_11 [label="11"];
    node_12 [label="12"];
    node_13 [label="13"];

    color=black;

    node_01 -> node_02 [label="f",shape=oval, color="green", fontcolor="green"];
    node_02 -> node_03 [label="o",shape=oval, color="green", fontcolor="green"];
    node_02 -> node_04 [label="l",shape=oval];
    node_03 -> node_05 [label="n",shape=oval];
    node_03 -> node_06 [label="r",shape=oval, color="green", fontcolor="green"];
    node_04 -> node_07 [label="o",shape=oval];
    node_05 -> node_08 [label="d",shape=oval];
    node_06 -> node_09 [label="t",shape=oval, color="green", fontcolor="green"];
    node_06 -> node_10 [label="m",shape=oval];
    node_07 -> node_11 [label="u",shape=oval];
    node_08 -> node_12 [label="u",shape=oval];
    node_10 -> node_13 [label="e",shape=oval];
}

这是一个简单的批处理文件,用于将 gv 文件转换为 svg

Here is a simple batch file to covert the gv file to an svg

SET PATH="C:\Program Files (x86)\Graphviz2.38\bin";PATH=%PATH%

dot -Tsvg so_question_07.gv -o so_question_07.svg

这篇关于Prolog - 用事实递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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