
- Prolog - Home
- Prolog - Introduction
- Prolog - Environment Setup
- Prolog - Hello World
- Prolog - Basics
- Prolog - Relations
- Prolog - Data Objects
- Loop & Decision Making
- Conjunctions & Disjunctions
Prolog Operators
- Prolog - Type of Operators
- Prolog - Arithmetic Comparison Operators
- Prolog - Unification Operators
- Prolog - Term Comparision Operators
- Prolog - Arithmetic Operators
- Prolog - Logical Operators
- Prolog - List Operators
- Prolog - Custom Operators
Prolog Lists
- Prolog - Lists
- Prolog - Member of List
- Prolog - Length of List
- Prolog - Concatenating Lists
- Prolog - Appending to a List
- Prolog - Deleting from a List
- Prolog - Inserting into a List
- Prolog - Permutation Operation
- Prolog - Combination Operation
- Prolog - Reverse Items of a List
- Prolog - Shift Items of a List
- Prolog - Check Order of a List
- Prolog - SubSet of a Set
- Prolog - Union of Sets
- Prolog - Intersection of Sets
- Prolog - Even and Odd Length Finding
- Prolog - Divide a List
- Prolog - Find Maximum of a List
- Prolog - Find Minimum of a List
- Prolog - Find Sum of a List
- Prolog - Sorting List using MergeSort
Built-In Predicates
- Prolog - Built-In Predicates
- Prolog - Identifying Terms
- Prolog - Decomposing Structures
- Prolog - Collecting All
- Prolog - Mathematical Predicates
- Prolog - Scientific Predicates
Miscellaneous
- Recursion and Structures
- Prolog - Backtracking
- Prolog - Preventing Backtracking
- Prolog - Different and Not
- Prolog - Inputs and Outputs
- Tree Data Structure (Case Study)
- Prolog - Examples
- Prolog - Basic Programs
- Prolog - Practical Arithmetic Examples
- Prolog - Examples of Cuts
- Towers of Hanoi Problem
- Prolog - Linked Lists
- Monkey and Banana Problem
- Prolog Useful Resources
- Prolog - Quick Guide
- Prolog - Useful Resources
- Prolog - Discussion
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 | ?-