无法在prolog中设置输入和输出

时间:2017-05-27 20:34:06

标签: prolog

我正在做一个prolog练习,取自this

我现在要做的是改变程序的输入和输出方式 我需要在控制台中输入以下命令来执行程序:

goldbach(100, L).

例如,我需要按[;]显示上一个结果在屏幕上打印时显示下一个结果。

L = [3, 97];
L = [11, 89];
L = ....

但是,我想做的是如下:

Input a number:100.
L = [3, 97].
L = [11, 89].
.....

即程序将首先打印“输入数字:”并读取您的输入,然后自动打印出所有可能的结果。

我已阅读有关read()和write的部分,但是当我添加这些部分时,我会失败:

read_gold :-
  write('Input a number:'),
  read(X),
  write(goldbach(X, L)).

如何修复代码以使程序实现我想要的输入和输出?谢谢你回答。

1 个答案:

答案 0 :(得分:2)

这样的事情将完全符合您的要求,尽管通常不会使用Prolog查询和解决方案。

var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    // Do something with the coordinates

    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;
}

一次性收集所有解决方案的更简洁方法是使用read_gold :- write('Input a number:'), read(X), show_results(goldbach(X)). show_results(Query) :- call(Query, L), write('L = '), write(L), write('.'), nl, fail. show_results(_). 列出它们:

findall/3

或者,没有明确提示:

read_gold(Solutions) :-
  write('Input a number:'),
  read(X),
  findall(L, goldbach(X, L), Solutions).

并将其查询为,例如:

read_gold(X, Solutions) :-
  findall(L, goldbach(X, L), Solutions).
相关问题