Hello World (Prolog)

From LiteratePrograms

Jump to: navigation, search
Other implementations: Ada | ALGOL 68 | Alice ML | Amiga E | Applescript | AspectJ | Assembly Intel x86 Linux | Assembly Intel x86 NetBSD | AWK | bash | BASIC | Batch files | C | C, Cairo | C, Xlib | Candle | Clojure | C++ | C# | Delphi | Dylan | E | Eiffel | Erlang | Forth | FORTRAN | Fortress | Go | Groovy | Haskell | Hume | IBM PC bootstrap | Inform 7 | Java | Java, Swing | JavaScript | LaTeX | Lisp | Logo | Lua | Maple | MATLAB | Mercury | OCaml/F Sharp | occam | Oz | Pascal | Perl | PHP | Pic | PIR | PLI | PostScript | Prolog | Python | Rexx | Ruby | Scala | Scheme | Seed7 | sh | Smalltalk | SQL | Standard ML | SVG | Tcl | Tcl Tk | Visual Basic | Visual Basic .NET | XSL

Prolog is a logic programming language that deduces new facts from a knowledge base consisting of facts and rules. The following rule prints a "Hello World!" message:

<<hello_world.pl>>=
hello_world :- write('Hello World!').

write/1 is a built-in predicate which writes a Prolog term to the standard output stream and always succeeds.

Now start the Prolog interpreter which will prompt you to enter a query. Type the following to load the source file:

?- consult(hello_world).
% hello compiled 0.00 sec, 612 bytes
Yes

consult/1 is another built-in predicate that will load the given file as a Prolog program.

Now make the following query:

?- hello_world.
Hello World!
Yes

The "Yes" is the response to your query and indicates that Prolog could deduce hello_world. This is because write always succeeds.

You could just as well have queried directly in the interpreter without writing a rule:

?- write('Hello World!').
Hello World!
Yes
Download code
Views
Personal tools