patternMinor
"Hello world!" in Prolog
Viewed 0 times
helloworldprolog
Problem
I have just written the following "hello world" program in Prolog. Although it is as simple as it can be, I would like your feedback as Prolog is very new to me.
Questions
prefered method, because it does not rely (so much) on a specific system.
As the code is short and I have difficulties with even getting that to work with Prolog, I would admire it if you could add the complete code when you make suggestions.
#!/usr/bin/swipl -q -t main -f
main :-
write("Hello world!"), nl, fail.Questions
- Shebang: I am used to use
#!/usr/bin/env pythonin Python. This is the
prefered method, because it does not rely (so much) on a specific system.
- write: it seems as if I could also use
format. Are there other options?
- Style guide: I have not found any style guide regarding indentation or spaces after
,.
- Is quitting with
failto usual way? I have seenhalt, too.
- File ending: I saved it as
helloworld.pro. What file extensions are common?
As the code is short and I have difficulties with even getting that to work with Prolog, I would admire it if you could add the complete code when you make suggestions.
Solution
Why fail at all? Isn't success better than failure? A shorter version is thus:
Also, why even have side effects? Isn't a declarative solution much better:
This way, you can also reason about the message within the program itself. This is not possible if it only appears as output on the terminal. You can ask on the toplevel whether there is any message:
File endings and shebang-lines are technicalities that are pretty unrelated to the program itself. A Prolog program that is intended as a shell script will often have an
This is a standardised feature, whereas placing arbitrary queries in the file is not.
main :- writeln('Hello world!').Also, why even have side effects? Isn't a declarative solution much better:
message('Hello World').This way, you can also reason about the message within the program itself. This is not possible if it only appears as output on the terminal. You can ask on the toplevel whether there is any message:
?- message(M).
M = 'Hello World'.File endings and shebang-lines are technicalities that are pretty unrelated to the program itself. A Prolog program that is intended as a shell script will often have an
initialization/1 directive somewhere in the file, like::- initialization(run).This is a standardised feature, whereas placing arbitrary queries in the file is not.
Code Snippets
main :- writeln('Hello world!').message('Hello World').?- message(M).
M = 'Hello World'.Context
StackExchange Code Review Q#59279, answer score: 6
Revisions (0)
No revisions yet.