Wednesday, November 25, 2009

Chapter 5 form E-book : Logic Programming Using PROLOG

5.1 Outputting terms


The write/1 predicate takes a single argument, which must be a valid Prolog

term. The built-in predicate nl/0 has also been used many times previously. It takes no arguments. Evaluating a nl goal causes a new line to be output to

the current output stream.

Example:

?- write(college),nl.

college

Yes

5.2 Inputting term

The built-in predicate read/1 is provided to input terms. It takes a single argument,

which must be a variable.

In the input stream, the term must be followed by a dot ('.') and at least one white space character, such as space or newline. The dot and white space characters are read in but are not considered part of the term.

If the argument variable is already bound the goal succeeds if and only if the

input term is identical to the previously bound value.

?- X=rooney,read(X).

: rooney.

X = rooney


5.3 Input and Output using computer

Although input and output of terms is easy, the use of quotes and full

stops can be not always suitable.

A much better approach for

problems of this kind is to input a character at a time. To do this it is first necessary

to know about the ASCII value of a character..

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.





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

EXAMPLE :
?- put(97),nl.
a
yes


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


Assuming the argument variable is unbound :
?- get0(N).
: Z
N = 90

If the argument variable is already bound,:

?- get0(X).
: a
X = 97

?- M is 41,get0(M).
: )
M = 41


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.

?- get(X).
: Z
X = 90


5.6 Using Characters


  1. 1. 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 *
Input and Output 75
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.

2.
ASCII
values of the input characters are not output, but the number of characters
(excluding the *) is output. The count predicate is defined with two arguments
which can be read as 'the number of characters counted so far' and 'the total number
of characters before the *

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



5.7 Input and Output Using Files

Prologue to take input from the current input stream and take the output from the current output stream. For example, the keyboard as input devices and monitors as a means of output. Input and output can also be raised through files and files. Users can open and close the input and output with a variety of files, but can only process one type of file it at the same time.

5.8 File Output : Changing the Current Output Stream

Predicate tell / 1
Current output stream can be changed using this Predicate. With this Predicate, the file was not opened will be deleted and replaced with the current output stream.

Predicate told / 0
This Predicate causes the file name changed to the user's terminal.

Predicate telling / 1
This Predicate causes the variable bound by the name of the current output stream.

5.9 File Input: Changing the Current Input Stream

Current input stream can be changed using the Predicate see, followed by the atom, as an example, see ( 'myfile.txt'). This Predicate causes the file name changed to current input stream. see the Predicate consists of several parts. First, the order seen / 0 which causes current input file into the user's terminal.
Reading from Files: End of File
If the end of the file found when evaluating Predicate get (X) or get0 (X), variable X will be bound to specific numerical values. If the range is 0-255 ASCII code, the code that appears before the code is 1.
Reading from Files: End of Record
End of the line indicated by karakterASCII input value 13. End of the record indicated by 2 values of 13 ASCII characters followed by 10.




No comments:

Post a Comment