12

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

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)

为什么?

4

2 回答 2

10

编辑

我做了更多的研究。显然,这就是 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.

?- 

请注意,您必须先查阅文件。我试过了,它肯定有效。

于 2010-03-31T20:33:43.077 回答
3

You need to name the procedure write, not Write. Upper case starting letters are for variables. (It might be less confusing if you call it something else like writeHi or something, so it doesn't have the same name as a built-in procedure, but it will still work when you call it write because your write has a different arity than the built in one).

Also you might want to replace "hi" with 'hi', though it will work either way (but only the second version will actually print the word hi to the screen - your version will print it as an integer list).

于 2010-03-31T20:42:48.903 回答