* Menampilkan output pernyataan
Untuk menampilkan output berupa pernyataan dapat digunakan predikat bawaan dari prolog, yaitu write, writeq, dan nl. Predikat write/1 (satu argumen) digunakan untuk menampilkan output tanpa tanda petik. Predikat writeq sama dengan predikat write, hanya saja writeq menampilkan pernyataan dengan disertai tanda petik (quote), kecuali jika pernyataan yang ditulis hanya satu kata saja, maka tanda petik tidak tertampilkan. Sedangkan predikat nl/0 (tanpa argumen) berfungsi sama dengan tombol enter, yaitu untuk membuat baris baru (new line).
Contoh penggunaan dari predikat-predikat tersebut adalah sebagai berikut.
1. Outputting Terms
The main built-in predicate provided for outputting terms is write/1. Examples:
?- write(26),nl.
26
yes
?- write('a string of characters'),nl.
a string of characters
yes
2. Inputting Terms
The built-in predicate read/1 is provided to input terms. It takes a single argument, which must be a variable. Example:
?- read(X).
: jim.
X = jim
: jim.
X = jim
?- X=fred,read(X).
: jim.
no
: jim.
no
3. Input and Output Using Characters
All printing characters and many non-printing characters (such as space and tab) have a corresponding ASCII (American Standard Code for Information Interchange) value, which is an integer from 0 to 255. Examples:
- 9 for tab
- 32 for space
- 65-90 for A to Z
- 97-122 for a to z
- 63 for (?)
4. Outputting Characters
Characters are output using the built-in predicate put/1. The predicate takes a single argument, which must be a number from 0 to 255 or an expression that evaluates to an integer in that range. Example:
?- put(97),nl.
a
yes
a
yes
5. Inputting Characters
Two built-in predicates are provided to input a single character: get0/1 and get/1. Examples:
?- get0(N).
: a
N = 97
: a
N = 97
?- M=dog,get0(M).
: )
no
6. Using Characters: Examples
The first example shows how to read in a series of characters from the keyboard finishing with * and to output their corresponding ASCII values one per line (for all characters excluding *).
The predicate readin is defined recursively. Process (X) will be taken until X have value 42 that signifying a * character.
The next example is predicate for count the number of character excluding the * as output.
Count of the number in "The time has come the walrus said" is 33 characters.
7. Input and Output Using Files
Prolog takes all input from the current input stream and writes all output to the current output stream. By default both of these are the stream named user, denoting the user's terminal, i.e. keyboard for input and screen for output.
The same facilities available for input and output from and to the user's terminal either term by term or character by character are also available for input and output from and to files (e.g. files on a hard disk or a CD-ROM).
The user may open and close input and output streams associated with any number of named files but there can only be one current input stream and one current output stream at any time. Note that no file can be open for both input and output at the same time (except user) and that the user input and output streams cannot be closed.
8. File Output: Changing the Current Output Stream
The current output stream can be changed using the tell/1 predicate, e.g. tell('outfile.txt'). The default current output stream is user, i.e. the user's terminal. This value can be restored either by using the told predicate or by tell(user).
The built-in predicate told/0 takes no arguments. Evaluating a told goal causes the current output file to be closed and the current output stream to be reset to user, i.e. the user's terminal.
The built-in predicate telling/1 takes one argument, which must be a variable and will normally be unbound. Evaluating a telling goal causes the variable to be bound to the name of the current output stream.
9. File Input: Changing the Current Input Stream
The current input stream can be changed using the see/1 predicate, e.g. see('myfile.txt').
The default current input stream is user, i.e. the user's terminal. This value can be restored either by using the seen predicate or by see(user).
The built-in predicate seen/0 takes no arguments. Evaluating a see goal causes the current input file to be closed and the current input stream to be reset to user, i.e. the user's terminal.
The built-in predicate seeing/1 takes one argument, which must be a variable and will normally be unbound. Evaluating a seeing goal causes the variable to be bound to the name of the current input stream.
9.1 Reading from Files: End of File
If the end of file is encountered when evaluating the goal read(X), variable X will be bound to the atom end_of_file.
If the end of file is encountered while evaluating the goal get(X) or get0(X), variable X will be bound to a 'special' numerical value.
9.2 Reading from Files: End of Record
Typically the end of a line of input at the user's terminal will be indicated by the character with ASCII value 13. The end of a record in a file will generally be indicated by two ASCII values: 13 followed by 10.
PRACTICAL EXERCISE
1. Predicate makelower (convert upper case to lower case).
The result in Prolog:
2. Predicate copyterms (copy form text file to another text file).
The input is:
The predicate is:

Tidak ada komentar:
Posting Komentar