1. Outputting Terms
The main built-in predicate provided for outputting terms is write/1. 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, It takes no arguments. Evaluating a nl goal causes a new line to be output to the current output stream. for examples;
?- write(26),nl.
26
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. Evaluating it causes the next term to be read from the current input stream,
which by default is the user's keyboard. When a read goal is evaluated, the input term is unified with the argument variable. If the variable is unbound (which is usually the case) it is bound to the input value. for example;
?- read(X).
: 26.
X = 26
If the argument variable is already bound (which for most users is far more likely to occur by mistake than by design), the goal succeeds if and only if the input term is identical to the previously bound value.
?- X=fred,read(X
: fred.
X = fred
3. Input and Output Using Characters
Although input and output of terms is straightforward, the use of quotes and full stops can be cumbersome and is not always suitable. A much better approach for problems of this kind is to input a character at a time.
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.
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. 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
5. Inputting Characters
The get0 predicate takes a single argument, which must be a variable. Evaluating a get0 goal causes a character to be read from the current input stream. The variable is then unified with the ASCII value of this character. 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
The get predicate takes a single argument, which must be a variable. The variable is then unified with the ASCII value of this character in the same way as for get0.
6. Input and Output Using Files