Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Inputs and Outputs



In this chapter, we will see some techniques to handle inputs and outputs through prolog. We will use some built in predicates to do these tasks, and also see file handling techniques.

Following topics will be discussed in detail −

  • Handling inputs and outputs

  • File handling using Prolog

  • Using some external file to read lines and terms

  • Character manipulation for input and output

  • Constructing and decomposing atoms

  • Consulting prolog files into other prolog program techniques.

Handling input and output

So far we have seen that we can write a program and the query on the console to execute. In some cases, we print something on the console, that are written in our prolog code. So here we will see that writing and reading tasks in more detail using prolog. So this will be the input and output handling techniques.

The write() Predicate

To write the output we can use the write() predicate. This predicate takes the parameter as input, and writes the content into the console by default. write() can also write in files. Let us see some examples of write() function.

Program

| ?- write(56).
56

yes
| ?- write('hello').
hello

yes
| ?- write('hello'),nl,write('world').
hello
world

yes
| ?- write("ABCDE").
[65,66,67,68,69]

yes
| ?- 

From the above example, we can see that the write() predicate can write the contents into the console. We can use nl to create a new line. And from this example, it is clear that, if we want to print some string on the console, we have to use single quotes string ('string'). But if we use double quote string ("string"), then it will return a list of ASCII values.

The read() Predicate

The read() predicate is used to read from console. User can write something in the console, that can be taken as input and process it. The read() is generally used to read from console, but this can also be used to read from files. Now let us see one example to see how read() works.

Program (read_write.pl)

cube :- write('Write a number: '), read(Number), process(Number).
process(stop) :- !.
process(Number) :- C is Number * Number * Number, write('Cube of '),write(Number),write(': '),write(C),nl, cube.

Output

| ?- consult('D:/TP Prolog/Sample Codes/read_write.pl').
compiling D:/TP Prolog/Sample Codes/read_write.pl for byte code...
D:/TP Prolog/Sample Codes/read_write.pl compiled, 2 lines read - 1226 bytes written, 5 ms

(15 ms) yes
| ?- cube.
Write a number: 2.
Cube of 2: 8
Write a number: 10.
Cube of 10: 1000
Write a number: 12.
Cube of 12: 1728
Write a number: 8.
Cube of 8: 512
Write a number: stop.

(63 ms) yes
| ?- 

The tab() Predicate

The tab() is one additional predicate that can be used to put some blank-spaces while we write something. So it takes a number as an argument, and prints those many number of blank spaces.

Program

| ?- write('hello'),tab(15),write('world').
hello               world

(16 ms) yes
| ?- write('We'),tab(5),write('will'),tab(5),write('use'),tab(5),write('tabs').
We     will     use     tabs

yes
| ?- 

Reading and Writing Files

Manipulating Chars

Handling Atoms

Advertisements