Prolog:制作打印Hello World的程序

时间:2010-03-31 20:29:45

标签: procedure swi-prolog prolog-toplevel

我想将这个简单的东西加载到我的编辑器中:

Write:-repeat,write("hi"),nl,fail.

因此它打印“hi”。

我该怎么办?

我目前正在尝试File->New

并将名为Write的文件保存到E:\Program Files\pl\xpce\prolog\lib

进行查询时:

- ?写

正在打印:

1 ?- Write.
% ... 1,000,000 ............ 10,000,000 years later
% 
%       >> 42 << (last release gives the question)

为什么?

2 个答案:

答案 0 :(得分:8)

修改

我做了更多的研究。显然,这就是当您询问有关未实例变量的SWI-Prolog时所做的事情。

$ prolog
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.6.64)
Copyright (c) 1990-2008 University of Amsterdam.
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- X.
% ... 1,000,000 ............ 10,000,000 years later
% 
%       >> 42 << (last release gives the question)
?- 

<强>更新

将名称更改为小写有效。大写用于变量:

helloworld.prolog:

helloworld:-write('Hello World!'),nl,fail.

然后:

$ prolog
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.6.64)
Copyright (c) 1990-2008 University of Amsterdam.
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?- ['helloworld.prolog'].
% helloworld.prolog compiled 0.00 sec, 1,376 bytes
true.

?- helloworld.
Hello World!
false.

?- 

请注意,您必须先查阅该文件。我尝试了这个,它确实有用。

答案 1 :(得分:3)

您需要将程序命名为write,而不是Write。大写起始字母用于变量。 (如果你把它称为writeHi之类的东西,它可能不那么令人困惑,因此它与内置程序没有相同的名称,但是当你调用它时它仍然可以工作{{1}因为你的写作具有与内置的不同的arity。)

此外,您可能希望将write替换为"hi",但它会以任何方式工作(但只有第二个版本实际上会将单词hi打印到屏幕上 - 您的版本会将其打印为整数列表)。