0% found this document useful (0 votes)
7 views

Lab4 LP

Uploaded by

Gezae Gebredingl
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab4 LP

Uploaded by

Gezae Gebredingl
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Logic Programming – Laboratory 4

I/O Operations with files

Isabela Drămnesc

Notions
• I/O in Prolog

1 Examples
1.1 Examples of writing

a ) ?− write ( ’ today ’ ) , write ( ’ i s s u n s h i n e ’ ) .

b ) ?− w r i t e q ( ” abc ” ) .
[ 9 7 , 98 , 99]
true .

c ) ?− write ( ’ abc ’ ) .
abc
true .

d ) ?− w r i t e q ( ’ abc ’ ) .
abc
true .

e ) ?− write ( ’ l a l a l a ’ ) .
la la la
true .

f ) ?− w r i t e q ( ’ l a l a l a ’ ) .
’ la la la ’
true .

g ) ?− w r i t e q ( ’ today ’ ) , w r i t e q ( ’ i s s u n s h i n e ’ ) .

/∗ f o r t o d a y d o e s not p u t q u o t e s ∗/

h ) ?− put ( 9 8 ) .
b
true .

1
/∗ p u t w r i t e s one c h a r a c t e r ( t h e r e p r e s e n t a t i o n i n t h e ASCII code ) ∗/

i ) ?− put ( ” 99 ” ) .
ERROR: put / 1 : Type e r r o r : ‘ c h a r a c t e r ’ e xpe ct ed , found ‘ [ 5 7 , 5 7 ] ’

j ) ?− put ( ’ 99 ’ ) .
ERROR: put / 1 : Type e r r o r : ‘ c h a r a c t e r ’ e xpe ct ed , found ‘ 9 9 ’

k ) ?− g e t (L ) .
| : force

L = 102

l ) ?− g e t (L ) .
|: f

L = 102

/∗ g e t r e a d s one s i n g l e c h a r a c t e r ∗/

m) ?− write ( today i s monday ) , put ( 8 ) , write ( bye ) .


today i s mondabye
true .

n ) ?− put ( 8 ) .
true .

o ) ?− write ( ’ h e l l o ’ ) , put ( 8 ) , write ( l a l a ) , put ( 9 8 ) , put ( 9 9 ) .


hellolalabc
true .

p ) ?− write ( h e l l o ) , put ( 3 2 ) , write (man ) .


h e l l o man
true .
1.2 Examples of reading

a ) ?− read (X ) .
| hello . /∗ t h i s i s what i n t r o d u c e s t h e u s e r ∗/

X = hello .
/∗ rea d u n i f i e s X w i t h what t h e u s e r i n t r o d u c e s ∗/

b ) ?−read ( l a l a ) .
| ’ today i s r a i n i n g ’ .
false .

c ) ?− read (X ) .
| today i s s u n s h i n e .

2
X = ( today i s s u n s h i n e ) .

d ) ?− read (X ) .
| : ’ today i s s u n s h i n e ’ .

X = ’ today i s s u n s h i n e ’ .
1.3 Examples of reading from files
Create a file pb.txt, then ask in Prolog:
a) the case when the file pb.txt is empty
?− see ( ’C: \ \ Documents and S e t t i n g s \\ Desktop \\pb . t x t ’ ) ,
read (X) , seen .

X = end of file .

?− see ( ’C: \ \ Documents and S e t t i n g s \\ Desktop \\pb . t x t ’ ) ,


read (X) , read ( I ) , seen .

X = end of file .
I = end of file .
b) the case when the file pb.txt contains
4.
5.
6.
7.
10.

?− see ( ’C: \ \ Desktop \\pb . t x t ’ ) , read (X) , read (Y) ,


read ( Z ) , read (W) , read (O) , seen .

X = 4,
Y = 5,
Z = 6,
W= 7,
O = 10.

1.4 Examples of writing in files

a ) ?− t e l l ( ’C: \ \ Desktop \\pb . t x t ’ ) ,


write ( ’ t h e f i r s t e x e r c i s e ’ ) , nl , write ( ’ i s s o l v e d ’ ) ,
write ( ’ t h e s e c o n d problem i s t o w r i t e i n t o a n o t h e r f i l e ’ ) ,
told .

true .

b ) ?− t e l l ( ’C: \ \ Desktop \\pb . t x t ’ ) ,


write ( ’ h e l l o ’ ) , tab ( 5 ) , write ( ’ a g a i n ’ ) , told .

3
true .

c ) ?− t e l l ( ’C: \ \ Desktop \\ pb2 . t x t ’ ) ,


write ( ’ t h e s e c o n d problem ’ ) , nl ,
write ( ’ i s t o w r i t e a p r e d i c a t e which c a l c u l a t e s
t h e sum o f a l l t h e numbers from a f i l e ’ ) , told .

true .

d ) ?− t e l l ( ’C: \ \ Desktop \\An2 . pdf ’ ) ,


write ( ’ w r i t e s i n pdf but open with notepad ’ ) , told .

true .

/∗ so we can w r i t e i n any k i n d o f f i l e s ∗/

2 Exercises
2.1 The sum
Read all the elements from the file pb.txt and print the sum of the elements.
a) printing the sum on the SWI-Prolog window
example :− see ( ’C: \ \ Desktop \\pb . t x t ’ ) ,
read (X) , read (Y) , read ( Z ) , read (V) , sum ( [ X, Y, Z ,V] ,W) ,
write ( ’ t h e sum o f t h e e l e m e n t s from t h e f i l e pb . t x t i s ’ ) ,
write (W) , seen .

sum ( [ ] , 0 ) .
sum ( [ X| T ] ,W): −sum (T, S ) ,W i s X+S .
b) print the sum of all the elements from the file pb.txt into another file
called sum.txt

2.2 Generate random lists


Write an efficient program (with accumulators) in Prolog which generates a list
of a certain length and its elements are random numbers. Examples:
? − generate elem list(10000000, 2, L).
will return the list L = [0, 0, 1, 0, 0, 1, 1, 0, 1, 0|.....] which has the length 10000000
and which contains binary elements.
? − generate elem list(10, 8, L). will return L = [5, 3, 0, 6, 6, 6, 5, 3, 3|...].

2.3 Merge–sort
The file pb.txt contains one single number on each line followed by dot. Create
a predicate in Prolog which sorts the numbers from the file pb.txt and print the
result into a file called sorted.txt.
Implement the merge-sort algorithm for integers in Prolog – informally it
can be formulated as follows: Given a list, divide the list into two halves. Sort

4
the halves and merge the two sorted lists. Sort the list (of the length 10000000)
generated at the previous exercise.

2.4 Left–shift
The file pb.txt contains one single number on each line followed by dot. Create
a predicate in Prolog which does shift-left (if we have in the file 4.2.3.1. after
we apply shift-left we will obtain 2.3.1.4.) and print the result into the file
changed1.txt.

2.5 Even–odd
The file pb.txt contains one single number on each line followed by dot. Create
a predicate in Prolog which separates the numbers from the file pb.txt into even
numbers and odd numbers. Even numbers are printed into the file even.txt, and
odd numbers are printed into the file odd.txt.

2.6 Prefixes
Write a program which returns all the prefixes of a list in 3 ways.
1. The recursive version;
2. Using accumulators;
3. Using open lists/difference lists.
Examples:
1. ?- prefix(L,[1,2,3,f,r,4]).
L = [] ;
L = [1] ;
L = [1, 2] ;
L = [1, 2, 3] ;
L = [1, 2, 3, f] ;
L = [1, 2, 3, f, r] ;
L = [1, 2, 3, f, r, 4] ;

Similar for prefix2 and prefix3.

3 Homework:
Consider as input data a file containing 100 lines. On each line there is e a
number followed by dot. Print into another file the sorted numbers (increasing
way)! Implement at least two sorting algorithms.

You might also like