Lab4 LP
Lab4 LP
Isabela Drămnesc
Notions
• I/O in Prolog
1 Examples
1.1 Examples of writing
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 ∗/
n ) ?− put ( 8 ) .
true .
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 .
X = end of file .
I = end of file .
b) the case when the file pb.txt contains
4.
5.
6.
7.
10.
X = 4,
Y = 5,
Z = 6,
W= 7,
O = 10.
true .
3
true .
true .
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.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] ;
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.