The main built-in predicate provided for outputting terms is write/1, which has
already been used many times in this book.
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 meaning of current output
70 Logic Programming With Prolog
stream will be explained in Sections 5.7 and 5.8. At present it can simply be taken
to mean the user's screen.)
The built-in predicate nl/0 has also been used many times previously in this
book. It takes no arguments. Evaluating a nl goal causes a new line to be output to
the current output stream.
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
Note that atoms that have to be quoted on input (e.g. 'Paul', 'hello world') are not
quoted when output using write. If it is important to output the quotes, the
writeq/1 predicate can be used. It is identical to write/1 except that atoms that
need quotes for input are output between quotes (other atoms are not).
?- writeq('a string of characters'),nl.
'a string of characters'
yes
?-writeq(dog),nl.
dog
yes
?- writeq('dog'),nl.
dog
yes
Input and Output 71
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 has also been used many times previously in this book. It takes no arguments. Evaluating a nl goal causes a new line to be output to the current output stream.
If it is important to output the quotes, the writeq/1 predicate can be used.
5.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. (The meaning of current input stream will
be explained in Sections 5.7 and 5.9. At present it can simply be taken to mean the
user's keyboard.)
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.
Note that for input from the keyboard (only) a prompt character such as a colon
will usually be displayed to indicate that user input is required. It may be necessary
to press the 'return' key before Prolog will accept the input. Both of these do not
apply to input from files.
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.
?- 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'
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.
72 Logic Programming With Prolog
?- X=fred,read(X).
: jim.
no
?- X=fred,read(X).
: fred.
X = fred
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. 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.
5.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. For example, it would be
tedious to define a predicate (using read) which would read a series of characters
from the keyboard and count the number of vowels. 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.
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.
Although input and output of terms is straightforward, the use of quotes and full stops can be cumbersome and is not always suitable. For example, it would be tedious to define a predicate (using read) which would read a series of characters from the keyboard and count the number of vowels. 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. The table below gives the numerical ASCII values corresponding to the main printable characters and some others.
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, for example
?- put(97),nl.
a
yes
?- put(122),nl.
z
yes
?- put(64),nl.
@
Yes
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.
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. The variable
is then unified with the ASCII value of this character.
Note that for input from the keyboard (only) a prompt character such as a colon
will usually be displayed to indicate that user input is required. It may be necessary
to press the 'return' key before Prolog will accept the input. Both of these also
apply to the get predicate described below but do not apply to input from files.
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
?- get0(N).
: Z
N = 90
74 Logic Programming With Prolog
?- get0(M)
: )
M = 41
If the argument variable is already bound, the goal succeeds if and only if it has
a numerical value that is equal to the ASCII value of the input character.
?- get0(X).
: a
X = 97
?- M is 41,get0(M).
: )
M = 41
?- M=dog,get0(M).
: )
no
?- M=41.001,get0(M).
: )
no
The get predicate takes a single argument, which must be a variable. Evaluating
a get goal causes the next non-white-space character (i.e. character with an ASCII
value less than or equal to 32) to be read from the current input stream. The
variable is then unified with the ASCII value of this character in the same way as
for get0.
?- get(X).
: Z
X = 90
?- get(M).
: Z
M = 90
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.
Assuming the argument variable is unbound (which will usually be the case), it
is bound to the ASCII value of the input character.
5.6 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 *
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. 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.)
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 next example is an extended version of the one above. This time 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 *'.
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
76 Logic Programming With Prolog
?- go(T).
: *
T = 0
The final example is a recursive program, based on the previous two, which
shows how to read in a series of characters ending with * and count the number of
vowels. Characters are read in one by one until a character with ASCII value 42
(signifying *) is encountered.
Here the two arguments of the 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.
The first two arguments of the processChar predicate can be interpreted in the
same way as for process, but the third argument is 'the number of vowels up to and
including the character (first argument)'.
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 */
Input and Output 77
vowel(111). /* o */
vowel(117). /* u */
?- go(Vowels).
: In the beginning was the word*
Vowels = 8
?- go(Vowels).
: pqrst*
Vowels = 0
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.)
The final example is a recursive program, based on the previous two, which shows how to read in a series of characters ending with * and count the number of vowels. Characters are read in one by one until a character with ASCII value 42 (signifying *) is encountered.
Here the two arguments of the 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.
The first two arguments of the processChar predicate can be interpreted in the same way as for process, but the third argument is 'the number of vowels up to and including the character (first argument)'.
Predicate vowel tests for one of the 10 possible vowels (five upper case and five lower case), using their ASCII values.
5.7 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 same facilities available for input and output from and to the user's
terminal either term by term or character by character are also available for input
and output from and to files (e.g. files on a hard disk or a CD-ROM).
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.
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 same facilities available for input and output from and to the user's terminal either term by term or character by character are also available for input and output from and to files (e.g. files on a hard disk or a CD-ROM).
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.
5.8 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.
78 Logic Programming With Prolog
Output to a File
Although the above definition of tell states that 'any existing file with the same
name is deleted', there is another possibility, which is important for some
applications, namely that the file is not deleted and any output is placed after the
end of the existing contents of the file. Both the 'overwrite' and the 'append' options
are likely to be available in any practical implementation of Prolog but may
involve using a different predicate (e.g. open) instead of or as well as tell. See the
documentation of your version of Prolog for details.
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.
5.9 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.
Note that the file corresponding to the previous current input stream remains
open when a new current input stream is selected. Only the current input stream
can be closed (using the seen predicate described below).
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.
5.9.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. 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.
5.9.2 Reading from Files: End of Record
Depending on the version of Prolog used, there may be an incompatibility for
character input between reading the end of a record (i.e. the character(s) that
signify the end of a line) from the user's terminal and from a file.
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.
The following program shows how to read in a series of characters from the
keyboard and print them out, one per line.
readline:-get0(X),process(X).
process(13).
process(X):-X=\=13,put(X),nl,readline.
Note the use of put rather than write and that the test for ASCII value 13
avoids the need for a character such as * to indicate 'end of input'.
?- readline.
: Prolog test
Pr
ol
og
t
est
yes
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. Note that the file corresponding to the previous current input stream remains open when a new current input stream is selected. Only the current input stream can be closed (using the seen predicate described below).
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 .
EXERCISE INPUT AND OUTPUT PROLOG
SOAL NO 1
Soal 2
Pertama, kita memasukkan input di notepad . Kemudian, disimpan dengan nama tes1.txt (format simpan tetap txt) di Local Disk C, seperti gambar di bawah ini :
Kemudian buka Prolog, dan jalankan fungsi copyterms seperti gambar dibawah ini, maka akan keluar output.txt secara langsung

Soal 3
Pertama kita menuliskan data seperti dibawah ini pada notepad

4. tuliskan printah untuk membaca file bernama testa.txt seperti

SOAL NO.4
1.membuat dua file dalam notepad seperti berikut (terakhir di beri kata'end')


2.kita akan menggabungkan isi karakter dari kedua file yang telah kita buat tadi dengan mengetik di notepad seperti ini







0 comments:
Post a Comment