<< >> Up Title Contents

3.1. Input and output

The most useful input output predicates are write/1, read/1 and nl/0.

write(term) is true if term is a Prolog term. As a side-effect, it cause term to appear on the current output device (e.g. your screen in the Worksheet Window). nl is always true, and as a side-effect, sends a newline instruction to the current output device. So the conjunction of a write/3 instruction and a nl/0 instruction will result in a term being displayed on the screen, followed by a carriage return.


read(X) is true if the user types a term followed by a full-stop. X becomes instantiated to the term. E.g.

| ?-read(X).
this.

X = this yes

read/1 can be used to input a list of words, but the list has to be typed complete with brackets and commas:

| ?-read(X).
[this,is,a,list].
X = [this,is,a,list] yes

Getting Prolog to accept a string of atoms and convert them into a list is quite heavy-duty Prolog programming and involves the use of get/1 and get/0, which accept single characters (in ASCII numerical format); the building of a list of such numbers; their conversion from a list of numbers into a Prolog atom, using the predicate name/2 and, finally the construction of a list of such atoms.


<< >> Up Title Contents