Wednesday, November 25, 2009

INPUT and OUTPUT in Prolog

Input and Output

Outputting Terms
Output form that be used in prolog is write/1 dan nl/0 predicate. Evaluating the predicate write/1 causes the term to be written to the current output stream and evaluating a nl goal causes a new line to be output to the current output stream.
Examples :
?- write('Example of use of nl'),nl,nl,write('end of example'),nl.
Example of use of nl
end of example
yes
if we want to show atoms, we can use writeq/1.
Examples :
?- writeq('a string of characters'),nl.
'a string of characters'
yes

Inputting Terms
Input form that be used in prolog is read/1. Evaluating it causes the next term to be read from the current input stream. 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.
Example :
?- 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'
?- X=fred,read(X).
: jim.
no
?- X=fred,read(X).
: fred.
X = fred

Input and Output Using Characters
There are many characters that can be used in input and output using prolog. All printing characters and many non-printing characters 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.

9 : tab
10 : end of record
32 : space
33 : !
34 : "
35 : #
36 : $
37 : %
38 : &
39 : '
40 : (
41 : )
42 : *
43 : +
44 : ,
45 : -
46 : .
47 : /
48-57 : 0 to 9
58 : :
59 : ;
60 : <
61 : =
62 : >
63 : ?
64 : @
65 : -
90 : A to Z
91 : [
92 : \
93 : ]
94 : ^
95 : _
96 : `
97 : -
122 : a to z
123 :{
124 : |
125 : }
126 : ~
Characters whose ASCII value is less than or equal to 32 are known as white space characters.

Outputting Characters
Predicate that be used for output characters is put/1 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.
Example :
?- put(97),nl.
a

Inputting Characters
Predicate that be used for input a single character are get0/1 and get/1. Evaluating a get0 goal causes a character to be read from the current input stream and evaluating a get goal causes the next non-white-space character to be read from the current input stream.
Example :
?- get0(N).
: a
N = 97
?- M is 41,get0(M).
: )
M = 41
?- M=dog,get0(M).
: )
No
?- get(X).
: Z
X = 90

Using Characters: Examples
The predicate readin is defined recursively. It causes a single character to be input and variable X to be bound to its 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.
Example :
readin:-get0(X),process(X).
process(42).
process(X):-X=\=42,write(X),nl,readin.
?- readin.
: Prolog Example*
80
114
111
108
111
103
32
69
120
97
109
112
108
101
yes
The 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 *'.
Example :
go(Total):-count(0,Total).
count(Oldcount,Result):-
get0(X),process(X,Oldcount,Result).
process(42,Oldcount,Oldcount).
process(X,Oldcount,Result):-
X=\=42,New is Oldcount+1,count(New,Result).
?- go(T).
: The time has come the walrus said*
T = 33
?- go(T).
: *
T = 0
Predicate vowel tests for one of the 10 possible vowels (five upper case and five lower case), using their ASCII values.
go(Vowels):-count(0,Vowels).
count(Oldvowels,Totvowels):-
get0(X),process(X,Oldvowels,Totvowels).
process(42,Oldvowels,Oldvowels).
process(X,Oldvowels,Totalvowels):-
X=\=42,processChar(X,Oldvowels,New),
count(New,Totalvowels).
processChar(X,Oldvowels,New):-vowel(X),
New is Oldvowels+1.
processChar(X,Oldvowels,Oldvowels).
vowel(65). /* A */
vowel(69). /* E */
vowel(73). /* I */
vowel(79). /* O */
vowel(85). /* U */
vowel(97). /* a */
vowel(101). /* e */
vowel(105). /* i */
vowel(111). /* o */
vowel(117). /* u */
?- go(Vowels).
: In the beginning was the word*
Vowels = 8
?- go(Vowels).
: pqrst*
Vowels = 0

Input and Output Using File
Prolog doing input and output in current stream form. The user may open and close input and output streams with any number of named files but there can only be one current input and output stream.

File Output: Changing the Current Output Stream
The current output stream can be changed using the tell/1 predicate. This value can be restored either by using the told/0 predicate or by tell(user).

File Input: Changing the Current Input Stream
The current input stream can be changed using the see/1 predicate which representing a file name, e.g. see('myfile.txt'). This value can be restored either by using the seen predicate or by see(user).

1. 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.
2. Reading from Files: End of Record
Typically the end of a line of input at the user's terminal will be indicated by the character with ASCII value 13. The end of a record in a file will generally be indicated by two ASCII values: 13 followed by 10.
readline:-get0(X),process(X).
process(13).
process(X):-X=\=13,put(X),nl,readline.
?- readline.
: Prolog test
P
r
o
l
o
g
t
e
s
t
yes

No comments:

Post a Comment