Prolog - Hello World



In the previous section, we have seen how to install GNU Prolog. Now, we will see how to write a simple Hello World program in our Prolog environment.

Hello World Program

After running the GNU prolog, we can write hello world program directly from the console. To do so, we have to write the command as follows −

write('Hello World').

Note − After each line, you have to use one period (.) symbol to show that the line has ended.

The corresponding output will be as shown below −

Hello World

Now let us see how to run the Prolog script file (extension is *.pl) into the Prolog console.

Before running *.pl file, we must store the file into the directory where the GNU prolog console is pointing, otherwise just change the directory by the following steps −

Step 1 − From the prolog console, go to File > Change Dir, then click on that menu.

Step 2 − Select the proper folder and press OK.

Select Working Directory

Now we can see in the prolog console, it shows that we have successfully changed the directory.

prolog_console

Step 3 − Now create one file (extension is *.pl) and write the code as follows −

main :- write('This is sample Prolog program'),
write(' This program is written into hello_world.pl file').

Now let’s run the code. To run it, we have to write the file name as follows −

[hello_world]

The output is as follows −

prolog_console1
Advertisements