Prolog: Making a procedure to print Hello World
Solution 1
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.
Solution 2
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).
Comments
-
andandandand over 4 years
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?
-
andandandand about 14 yearsI don't think so, I changed the program to Write:-write("hi") and it's doing the same thing.
-
andandandand about 14 yearsAlright, how do I print anything? I want to do a simple "hello world".
-
andandandand about 14 yearsNah, I changed the clause to writehello:-write("hi"). and now it's printing: ERROR: Undefined procedure: writehello/0. I think the problem is the physical location where I'm saving the clause. Btw, I'm not sure but I think it's more correct to call it "clause" than "procedure".
-
sepp2k about 14 years@dmindreader: Well, you have to load (
consult
) the file before you try to invoke it. I guarantee you that it works with the lower case name. I actually tried it. -
Vivin Paliath about 14 yearsYou can't start a procedure with an uppercase letter. Change your code to
hello_world:-write('hello world'),nl,fail.
Notice that I'm starting the procedure with a lower-case letter. -
andandandand about 14 yearsIn what directory are you saving helloworld.prolog? Why are you using the .prolog extension? My SWI only recognizes files with the .pl extension.
-
Vivin Paliath about 14 yearsIn the same directory where I'm calling
prolog
. I used.prolog
as an arbitrary extension. SWI Prolog didn't complain. You can change it to.pl
and it should still work.