Summary For Chapter 5

built-in predicates that read from and write well for users
terminal (keyboard and screen) or a file, the second term by term and characterby --
characters in your own programs. ASCII value for manipulating character strings. Prolog has a facility to enable both input and output of the term or character. Using simple terms and will be explained first. Initially, it will be assumed that
all the output to the screen the user and all user input is the keyboard. Inputs and outputs use external files.


Output Terms

predicate takes one argument, which must be a valid Prolog term. Evaluate the causes predicate terms to be written to the output current
river, which by default is the user's screen. (The meaning of output current Logic Programming With Prolog
streaming)

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


Inputting Terms

Built-in predicate read is provided to include the term. It takes one argument, which must be variable. Evaluating the causes for the next term read from the input stream, which by default is the user's keyboard. (The meaning of the input current). In the input stream, the term must be followed by a dot ('.') and at least one space, such as spaces or new lines. Point and space characters are read in but not considered part of the term.

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]]
?- read(Y).
: 'a string of characters'.
Y = 'a string of characters'


Input and Output Using a Character

Although the input and output of the conditions is very easy, use quotation marks and full stops can be complicated and not always appropriate. For example, would be tedious to determine the predicate (using read) that will read a series of characters from the keyboard and count the number of vowels. A better approach for such problems is to input a character at a time. To do this, first of all necessary
to know about the value of ASCII characters.

Outputting Character

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
?- put(122),nl.
z
yes
?- put(64),nl.
@
yes

Inputting Characters

Two built-in predicates are provided to input a single character: get0/1 and get/1.
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.

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. It causes a single character to be
input and variable X to be bound to its (numerical) ASCII value. The action taken
(the process(X) goal) depends on whether or not X has the value 42 signifying a *

character. If it has, the evaluation of the goal stops. If not, the value of X is output,
followed by a new line, followed by a further call to readin. This process goes on
indefinitely until a * character is read. (In the example below, the ASCII values of
characters P, r, o etc. are correctly shown to be 80, 114, 111 etc.)

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 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

The current output stream can be changed using the tell/1 predicate. This takes a
single argument, which is an atom or variable representing a file name, e.g.
tell('outfile.txt').
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

The current input stream can be changed using the see/1 predicate. This takes a
single argument, which is an atom or variable representing a file name, e.g.
see('myfile.txt').
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.
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.


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. As ASCII values must be in
the range 0 to 255 inclusive, this will typically be -1, but may vary from one
implementation of Prolog to another.

Tidak ada komentar:

Posting Komentar