Program 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. Now let us see one example to see how read() works. The read() is generally used to read from console, but this can also be used to read from files. User can write something in the console, that can be taken as input and process it. The read() predicate is used to read from console. But if we use double quote (“string”), then it will return a list of ASCII values. 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‘). Program | ?- write(56).56yes| ?- write('hello').helloyes| ?- write('hello'),nl,write('world').helloworldyes| ?- write("ABCDE").yesįrom the above example, we can see that the write() predicate can write the contents into the console. Let us see some examples of write() function. This predicate takes the parameter as input, and writes the content into the console by default. To write the output we can use the write() predicate. So this will be the input and output handling techniques. So here we will see that writing and reading tasks in more detail using prolog. In some cases, we print something on the console, that are written in our prolog code. So far we have seen that we can write a program and the query on the console to execute. Using some external file to read lines and termsĬharacter manipulation for input and outputĬonsulting prolog files into other prolog program techniques. We will use some built in predicates to do these tasks, and also see file handling techniques.įollowing topics will be discussed in detail − In this chapter, we will see some techniques to handle inputs and outputs through prolog.
0 Comments
Leave a Reply. |