Prolog - 没有返回所需的值 [英] Prolog - Not returning desired value

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

问题描述

这是一个简单的 Prolog 程序,用于展示我目前在实际代码中遇到的问题.

我正在尝试在 Prolog 中编写一个 Brainf*ck 解释器,但它没有返回正确的 Output

我正在运行 bf("+.", Output)

预期回报 - Output = [1]

实际回报 - Output = [].

Output 在返回到 bf2 中的 iterateProg(Prog,Output) 时丢失其值.

程序如下:

bf2(Prog,Output):-iterateProg(Prog,Output).iterateProg([],_):- !.iterateProg(Prog, Output):-checkInstruction(Prog,Output, NewProg, NewOutput),iterateProg(NewProg, NewOutput).checkInstruction([Plus|ProgTail],Output,ProgTail,Output):-char_code('+',Plus)% 将内存增加 1 以便稍后打印..checkInstruction([Period|ProgTail], Output, ProgTail, NewOutput):-char_code('.',Period),%取刚才加法的内存中的值,我们假设它是1追加(输出,[1],新输出).

解决方案

你的谓词 bf2(Prog, Output) 的意思是:这个程序 Prog 对应这个output Output - 所以 bf2 谓词和 iterateProg 中的 Output 应该包含完整的输出.>

因此在checkInstruction谓词中,前2个参数应包含完整程序和完整输出,后2个参数应包含部分程序和相应的部分输出.

因此,您应该将 [1] 附加到 NewOutput,以在 Output 中为 checkInstruction 谓词.您应该使用不同的名称重命名 New... - 因为它实际上是部分/尾部"程序和相应的输出.

append([1], NewOutput, Output)

注意顺序(我在评论中不准确).[1] 是当前指令的输出,它应该在ProgTail中指令的输出之前.

顺便说一下,bf2 谓词是不必要的 - 因为它调用 iterateProg 谓词并使用完全相同的参数.

另一件事是 iterateProg([],_) 应该是 iterateProg([],[]) 因为一个空程序应该对应于空输出.(反之则不然,所以如果您已经了解了它,则永远不要在此处添加剪切).

Here is a simple Prolog program to exhibit a problem that I currently have with my real code.

I am trying to write a Brainf*ck interpreter in Prolog, but it is not returning the right Output

I am running bf("+.", Output)

Expected Return - Output = [1]

Actual Return - Output = [].

Output looses its value when it returns to iterateProg(Prog,Output) in bf2.

Program is as follows:

bf2(Prog,Output):-
    iterateProg(Prog,Output).

iterateProg([],_):- !.
iterateProg(Prog, Output):-
    checkInstruction(Prog,Output, NewProg, NewOutput),
    iterateProg(NewProg, NewOutput).

checkInstruction([Plus|ProgTail],Output,ProgTail,Output):-
    char_code('+',Plus)
    %increase memory by 1 for printing later.
    .

checkInstruction([Period|ProgTail], Output, ProgTail, NewOutput):-
    char_code('.',Period),
    %take the value in memory from addition just now, we will assume it is 1
    append(Output,[1],NewOutput).

解决方案

Your predicate bf2(Prog, Output) has the meaning of: this program Prog correspond with this output Output - so the Output in the bf2 predicate and also in iterateProg should contains the full output.

So in checkInstruction predicate, the first 2 arguments should contain the full program and the full output, and the last 2 arguments should contain the partial program and the corresponding partial output.

Therefore, you should append the [1] with NewOutput, to produce the final output in Output for the second rule of checkInstruction predicate. You should rename the New... with a different name - since it is actually the partial/"tail" program and corresponding output.

append([1], NewOutput, Output)

Note the order (I was inaccurate in the comment). [1] is the output for the current instruction, and it should precede the output of the instruction in ProgTail.

By the way, bf2 predicate is unnecessary - since it call iterateProg predicate with exact same arguments.

Another thing is iterateProg([],_) should be iterateProg([],[]) since an empty program should correspond with empty output. (The converse is not true, so you should never put a cut ! here, if you have learned about it).

这篇关于Prolog - 没有返回所需的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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