INPUT and OUTPUT in PROLOG ch.1
Posted by madmathc pada Desember 22, 2009
Huff… so long I didn’t write at this blog because many assignments must be done and because these two weeks is the final week of 1st semester, projects and final test are coming to me. T.T….But now it is time for me to show u how to finish Practical Exercise in chapter INPUT & OUTPUT. These are the question :
(1) Define a predicate makelower/0 which reads in a line of characters from the keyboard and outputs it again as a single line with any upper case letters converted to lower case. (The ASCII values of the characters a, z, A and Z are 97, 122, 65 and 90, respectively.)
Thus the following would be a typical use of makelower:
: This is an Example 123 inCLUDing numbers and symbols +-*/@ XYz
this is an example 123 including numbers and symbols +-*/@ xyz
(2) Define a predicate copyterms which reads all the terms in a text file and outputs them as terms to another text file one by one on separate lines. The output file should be in a format suitable for use as the input file in a subsequent call of copyterms. Thus for example if the input file contained
‘first term’. ‘second term’.
fourth. ‘fifth term’.
The output file would contain
(3) Create a text file testa.txt containing two lines, each of five characters followed by a new line, e.g.
Define a predicate readfile that will read fifteen characters from this file one by one and output the ASCII value of each character. Use this to establish whether the representations of ‘end of file’ and ‘end of record’ for your version of Prolog are as suggested in Sections 5.9.1 and 5.9.2, respectively.
(4) Using a text editor, create two text files in1.txt and in2.txt, each comprising a number of terms terminated by end. Define and test a predicate combine that takes the names of two input files as its first two arguments and the name of an output file as its third argument. The output file should contain the terms in the first input file followed by the terms in the second, one per line and terminated by end.
(5) Define and test a predicate compare that reads in two text files term by term and for each pair of corresponding terms outputs a message either saying that they are the same or that they are different. Assume that both files contain the same number of terms and that the final term in each is end.
And now the solution of those problem are…..
(1) Type this in notepad and save. Don’t forget to add .pl at the end of your filename.
Then consult on Prolog.
This is an Example 123 inCLUDing numbers and symbols +-*/@ XYz
then press Enter. The output shoud be here