Tampilkan postingan dengan label Tutorial. Tampilkan semua postingan
Tampilkan postingan dengan label Tutorial. Tampilkan semua postingan

INPUT AND OUTPUT in Prolog

Problem 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.)

1.Make a notepad/txt file like the picture below, then save it in .pl format


Problem 1

2.Then consult it in prolog, then type "makelower. Input the letter to be lowercased , and the result will be like this.




Problem 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'.
'third term'.
fourth. 'fifth term'.
sixth.
The output file would contain
'first term'.
'second term'.
'third term'.
fourth.
'fifth term'.
sixth.

1. Make infile (save in .txt) and outfile (save in .pl) like in the picture.








2.Then consult .pl file then type copyterms('Infile.txt','Outfile.txt').




3. The result will be like this




Problem 3

Create a text file testa.txt containing two lines, each of five characters followed
by a new line, e.g.
abcde
fghij
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 Input and Output 83 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.

1.make a new prolog file (.pl) like in the picture



2.Then make one .txt file that contain character like in the picture



3.Then consult your prolog file then type readfile('testa.txt'). Then the result will be shown like this :




Problem 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.

1.make a prolog code like in the picture




2.then make 2 .txt that contain something we want to combine





3. Then consult the prolog file then type combine('In1.txt','In2.txt','Hasil.txt').



4.And the result will be made into hasil.txt

OPERATOR AND ARITHMATIC in prolog

pada bab ini diperkenalkan notasi operator predikat dan menggambarkan operator
disediakan untuk mengevaluasi dan membandingkan nilai-nilai ekspresi aritmatika, untuk
pengujian untuk kesetaraan aritmatika baik ungkapan atau istilah-istilah dan untuk pengujian untuk
negasi dari tujuan atau pemisahan dari dua gol.

1. Operator

Binary predicate atau predikat dengan dua argumen dapat diubah menjadi bentuk infix operator. Contoh :
Standar : makan (kucing,tikus)
infix operator : kucing makan tikus

Unary predikat (predikat dengan satu argumen) dapat diubah menjadi bentuk prefix operator atau postfix operator. Contoh :
Standar : lucu (anjing)
prefix operator : lucu anjing
postfix operator: anjing lucu

2. Aritmatika
Dengan Prolog kita dapat melakukan penghitungan aritmatika.

a. Operator Aritmatika
X+Y (penjumlahan)
X-Y (pengurangan)
X*Y (perkalian)
X/Y (pembagian)
X//Y the ‘integer quotient’ of X and Y (the result is truncated to the nearest integer between it and zero)
X^Y (pangkat)
-X (negatif)
abs(X) (nilai absolut)
sin(X) (sinus)
cos(X) (cosinus)
max(X,Y)(nilai terbesar)
sqrt(X) (akar)

b. Pengutamaan Operator Dalam Ekspresi Aritmatika
Prolog menggunakan algoritma aljabar biasa dalam pengopersian aritmatika. Contohnya A+B*C-D. Di dalam ajabar C dan D dikalikan lebih dahulu lalu ditambah dengan A lalu dikurangi dengan D. DI prolog juga demikian. Untuk pengecualian, kita tinggal menggunakan kurung. Contoh : (A+B)*(C+D).

c. Operator Relasi
Operator seperti =, !=, >,>=, <, =<, dapat digunakan di Prolog.

3. Operator Pembanding

Berikut merupakan daftar dari equality operators yang digunakan dalam prolog beserta fungsi dari masing-masing operator.
Arithmetic Expression Equality =:=
Arithmetic Expression Inequality =\=
Terms Identical ==
Terms Not Identical \==
Terms Identical With Unification =
Non-Unification Between Two Terms \=

4. Operator Logika


a. Operator Not
Operator not dapat ditempatkan sebelum predikat untuk memberikan negasi. Predikat yang dinegasikan bernilai benar jika predikat yang asli salah dan bernilai salah jika predikat yang asli benar. Contoh penggunaan operator not :
dog(fido).
?- not dog(fido).
no
?- dog(fred).
no
?- not dog(fred).
yes
b. Operator Disjungsi
Operator disjungsi (;) digunakan sebagai operator ‘atau’. Contoh :
?- 6<3;7 is 5+2.
yes
?- 6*6=:=36;10=8+3.
yes



Hasil Practical Exercise 4



Operator and aritmatic



langkah 1 :
buat konfigurasi angka dengan memasukkan rumus rata-rata (X+Y/2), nilai kali, dan nilai terbesar.



langkah 2 :
ketik "number(angka,angka)."
untuk menjalankan prolog.

Fact, Rules, Predicate, and Variable in Prolog

=-Animals Database -=

animal(mammal,tiger,carnivore,stripes).
animal(mammal,hyena,carnivore,ugly).
animal(mammal,lion,carnivore,mane).
animal(mammal,zebra,herbivore,stripes).
animal(bird,eagle,carnivore,large).
animal(bird,sparrow,scavenger,small).
animal(reptile,snake,carnivore,long).
animal(reptile,lizard,scavenger,small).

1.all the mammals

?- animal(mammal,A,_,_).

2.all the carnivores that are
mammals

?-animal(mammal,A,carnivore,_).

3.all the mammals with stripes

?- animal(mammal,A,_,stripes).

4.whether there is a reptile that has a
mane

?- animal(reptile,_,_,mane).


/* Dating Agency Database */
person(bill,male).
person(george,male).
person(alfred,male).
person(carol,female).
person(margaret,female).
person(jane,female).
couple(X,Y):-person(X,male),person(Y,female). ---->revision

Extend the program with a rule that defines a predicate couple with two
arguments, the first being the name of a man and the second the name of a woman.
Load your revised program into Prolog and test it.

?- couple(X,Y).