如何从问题中打印变量值? [英] How to print variable value from a question?

查看:38
本文介绍了如何从问题中打印变量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一点补充:

sumbit(CIN,A,B,CO,R):- ...?- 求和(0,1,1,一氧化碳,R),写(CIN),NL,写(A),nl,write("+"),nl,写(B),nl,write("--"),nl,write(CO),write(R),nl.

我想要做的是打印 CIN、A、B、CO 和 R 的变量值.它应该是这样的:

<代码>01+1——10

结果是这样的:

_40_73+_149——10是的.

还有没有办法不打印是"?如果有帮助,我正在使用草莓序言.提前致谢

解决方案

在不改变谓词定义的情况下实现这一目标的一种方法是调整查询,如下所示:

?- [CIN, A, B] = [0, 1, 1],sumbit(CIN,一种,乙,一氧化碳,R),写(CIN),NL,写(A),nl,write("+"),nl,写(B),nl,write("--"),nl,写(CO),写(R),nl.

现在所有变量都被实例化,要么通过调用本身,要么在调用之前.

当一个变量没有被实例化时,没有要打印的值,所以它的名称"被打印出来.但由于未使用的名称本身没有任何意义,系统可以随意将其重命名为任何名称.在 SWI 序言中:

1 ?- write(A)._G1338真的.

重命名通常作为 Prolog 问题解决过程的一部分进行,以确保同一谓词的任何两个单独调用不会相互干扰.

因此,在 SWI Prolog 使用诸如 _G1338 之类的名称的地方,您使用的 Prolog 实现显然仅使用带有数字的名称,在下划线之后,例如 _40.

I´m making a one bit addition:

sumbit(CIN,A,B,CO,R):- ... ?- sumbit(0 ,1 ,1 ,CO ,R) ,write(CIN),nl ,write(A),nl ,write("+"),nl ,write(B),nl ,write("--"),nl ,write(CO),write(R),nl.

What I want to do is to print the variable values of CIN,A,B,CO and R. It should come out something like this:

0
1
+
1
--
10

Instead it comes out as this:

_40
_73
+
_149
--
10
Yes.

Also is there a way to not print the "Yes"? I´m using strawberry prolog if it helps. Thank you in advance

解决方案

One way to achieve that without altering your predicate definition is to tweak the query, like so:

?- [CIN, A, B] = [0, 1, 1]
    ,sumbit(CIN
        ,A
        ,B
        ,CO
        ,R)
    ,write(CIN),nl
    ,write(A),nl
    ,write("+"),nl
    ,write(B),nl
    ,write("--"),nl
    ,write(CO),write(R),nl.

Now all variables are instantiated, either by the call itself, or prior to the call.

When a variable is not instantiated, there's no value to print, so its "name" is printed instead. But since non-used name has no meaning in itself, it can be freely renamed by the system to anything. In SWI Prolog:

1 ?- write(A).
_G1338
true.

The renaming is usually done, as part of the Prolog problem solving process, to ensure that any two separate invocations of the same predicate do not interfere with each other.

So where SWI Prolog uses names like _G1338, the Prolog implementation you're using evidently uses names with the numbers only, after the underscore, like _40.

这篇关于如何从问题中打印变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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