Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog: Making a procedure to print Hello World

I want to load this simple something into my Editor:

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

So that it prints "hi".

What should I do?

I'm currently trying to do File->New

and Saving a file named Write into E:\Program Files\pl\xpce\prolog\lib

When doing the query:

?-Write.

It's printing:

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

Why?

like image 483
andandandand Avatar asked Mar 31 '10 20:03

andandandand


2 Answers

EDIT

I did some more research. Apparently this is what SWI-Prolog does when you ask it about an uninstantiated variable.

$ 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)
?- 

UPDATE

Changing the name to lowercase works. Uppercase is for variables:

helloworld.prolog:

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

Then:

$ 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.

?- 

Notice that you have to consult the file first. I tried this out and it works for sure.

like image 125
Vivin Paliath Avatar answered Oct 13 '22 21:10

Vivin Paliath


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).

like image 22
sepp2k Avatar answered Oct 13 '22 23:10

sepp2k