- Outputting Terms
The write/1 predicate takes a single argument, which must be a valid Prolog term. Evaluating the predicate causes the term to be written to the current output stream, which by default is the user's screen. The built-in predicate nl/0 has also been used many times previously in this book. It takes no arguments. Evaluating a nl goal causes a new line to be output to the current output stream.
Examples
?- write(26),nl.
26
yes
?- write('a string of characters'),nl.
a string of characters
yes
?- write([a,b,c,d,[x,y,z]]),nl.
[a,b,c,d,[x,y,z]]
yes
?- write(mypred(a,b,c)),nl.
mypred(a,b,c)
yes
?- write('Example of use of nl'),nl,nl,write('end of example'),nl.
Example of use of nl
end of example
yes
If it is important to output the quotes, the writeq/1 predicate can be used.
- Inputting Terms
example :
?- read(X).
: jim.
X = jim
?- read(X).
: 26.
X = 26
?- read(X).
: mypred(a,b,c).
X = mypred(a,b,c)
?- read(Z).
: [a,b,mypred(p,q,r),[z,y,x]].
Z = [a,b,mypred(p,q,r),[z,y,x]]
- 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. The table below gives the numerical ASCII values corresponding to the main printable characters and some others.
- Outputting Characters
Evaluating a put goal causes a single character to be output to the current output stream. This is the character corresponding to the numerical value (ASCII value) of its argument, for example
?- put(97),nl.
a
yes
?- put(122),nl.
z
yes
- Inputting Characters
Assuming the argument variable is unbound (which will usually be the case), it
is bound to the ASCII value of the input character.
?- get0(N).
: a
N = 97
?- get0(N).
: Z
N = 90
- Using Characters: Examples
The final example is a recursive program, based on the previous two, which shows how to read in a series of characters ending with * and count the number of vowels. Characters are read in one by one until a character with ASCII value 42 (signifying *) is encountered.
Here the two arguments of the count predicate can be interpreted as 'the number of vowels so far' and 'the total number of vowels'. The three arguments of the process predicate can be read as 'the ASCII value of an input character', 'the number of vowels up to but not including that character' and 'the total number of vowels', respectively.
The first two arguments of the processChar predicate can be interpreted in the same way as for process, but the third argument is 'the number of vowels up to and including the character (first argument)'.
Predicate vowel tests for one of the 10 possible vowels (five upper case and five lower case), using their ASCII values.
- Input and Output Using Files
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.
- File Output: Changing the Current Output Stream
Evaluating a tell goal causes the named file to become the current output stream. If the file is not already open, a file with the specified name is first created (any existing file with the same name is deleted).
Note that the file corresponding to the previous current output stream remains open when a new current output stream is selected. Only the current output stream can be closed (using the told predicate described below).
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.
- File Input: Changing the Current Input Stream
Evaluating a see goal causes the named file to become the current input stream. If the file is not already open it is first opened (for read access only). If it is not possible to open a file with the given name, an error will be generated. Note that the file corresponding to the previous current input stream remains open when a new current input stream is selected. Only the current input stream can be closed (using the seen predicate described below).
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 .