Prolog: Making a procedure to print Hello World
Asked Answered
L

2

12

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?

Lasandralasater answered 31/3, 2010 at 20:29 Comment(2)
By the way, that "error message" is a reference to The Hitchhiker's Guide To The Galaxy.Sayer
I know. I love those books, but I find that message pretty dumb. It's a forced and obfuscating joke.Lasandralasater
G
10

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.

Gaga answered 31/3, 2010 at 20:33 Comment(5)
I don't think so, I changed the program to Write:-write("hi") and it's doing the same thing.Lasandralasater
Alright, how do I print anything? I want to do a simple "hello world".Lasandralasater
You 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.Gaga
In what directory are you saving helloworld.prolog? Why are you using the .prolog extension? My SWI only recognizes files with the .pl extension.Lasandralasater
In 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.Gaga
C
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).

Capsaicin answered 31/3, 2010 at 20:42 Comment(2)
Nah, 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".Lasandralasater
@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.Capsaicin

© 2022 - 2024 — McMap. All rights reserved.