Tuesday, December 22, 2009

Expert System Final Project

This is the link to download Our Final Project Report..

Click Here to Download

If We make mistake please let us know Sir !!

Because We are a newbie.. (^_^)

Read more...

Sunday, December 13, 2009

Expert System Application

Expert System which we made is about horoscope. Horoscope is a prediction of someone's future based on the relative positions of the planets (Source : Google).

Everyone absolutely have horoscope. So, We want to help everyone to know his/her horoscope by entering date and month of their birthday.

The logic to make the program which can show a horoscope, the program must have a function to receive input date and month of their birthday. Then the program will check the program like this :

- Date of born 22 December -19 January, the program conclude the horoscope is Capricorn.
- Date of born 20 January -18 February, the program conclude the horoscope is Aquarius.
- Date of born 19 February -20 March, the program conclude the horoscope is Pisces.
- Date of born 21 March -19 April, the program conclude the horoscope is Aries.
- Date of born 20 April - 20 May, the program conclude the horoscope is Taurus.
- Date of born 21 May - 21 June, the program conclude the horoscope is Gemini.
- Date of born 22 June - 22 July, the program conclude the horoscope is Cancer.
- Date of born 23 July - 22 August, the program conclude the horoscope is Leo.
- Date of born 23 August -22 September, the program conclude the horoscope is Virgo.
- Date of born 23 September-23 October, the program conclude the horoscope is Libra.
- Date of born 24 October – 21 November, the program conclude the horoscope is Scorpio.
- Date of born 22 November- 21 December, the program conclude the horoscope is Sagittarius.

So, We can write the source code like this :

get_answer(Ans1,Ans2):-repeat,write('Enter the date (ended by writing dot mark) : '),read(Ans1),write('Enter your birth month (ended by writing dot mark) : '),read(Ans2),member(Ans1,[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]),member(Ans2,[january,february,march,april,may,june,july,august,september,october,november,december]),nl,nl,write('Your birthday is '),write(Ans1),write(' '),write(Ans2),nl,(Ans2==january,Ans1<20,write('So, Your Horoscope is : Capricorn'),nl;Ans2==february,Ans1>28,write('It is Imposible, because February just have 28 days !!'),nl;Ans2==december,Ans1>21,write('So, Your Horoscope is : Carpicorn'),nl;Ans2==january,Ans1>19,write('So, Your Horoscope is : Aquarius'),nl;Ans2==february,Ans1<19,write('So, Your Horoscope is : Aquarius'),nl;Ans2==february,Ans1>18,Ans1<29,write('So, Your Horoscope is : Pisces'),nl;Ans2==march,Ans1<21,write('So, Your Horoscope is : Pisces'),nl;Ans2==march,Ans1>20,write('So, Your Horoscope is : Aries'),nl;Ans2==april,Ans1<20,write('So, Your Horoscope is : Aries'),nl;Ans2==april,Ans1>19,Ans1<31,write('So, Your Horoscope is : Taurus'),nl;Ans2==april,Ans1==31,write('It is Imposible, because April just have 30 days !!'),nl;Ans2==may,Ans1<21,write('So, Your Horoscope is : Taurus'),nl;Ans2==may,Ans1>20,write('So, Your Horoscope is : Gemini'),nl;Ans2==june,Ans1<22,write('So, Your Horoscope is : Gemini'),nl;Ans2==june,Ans1>21,Ans1<31,write('So, Your Horoscope is : Cancer'),nl;Ans2==june,Ans1==31,write('It is Imposible, because June just have 30 days !!'),nl;Ans2==july,Ans1<23,write('So, Your Horoscope is : Cancer'),nl;Ans2==july,Ans1>22,write('So, Your Horoscope is : Leo'),nl;Ans2==august,Ans1<23,write('So, Your Horoscope is : Leo'),nl;Ans2==august,Ans1>22,write('So, Your Horoscope is : Virgo'),nl;Ans2==september,Ans1<23,write('So, Your Horoscope is : Virgo'),nl;Ans2==september,Ans1>22,Ans1<30,write('So, Your Horoscope is : Libra'),nl;Ans2==september,Ans1==31,write('It is Imposible, because September just have 30 days !!'),nl;Ans2==october,Ans1<24,write('So, Your Horoscope is : Libra'),nl;Ans2==october,Ans1>23,write('So, Your Horoscope is : Scorpio'),nl;Ans2==november,Ans1<23,write('So, Your Horoscope is : Scorpio'),nl;Ans2==november,Ans1>21,Ans1<31,write('So, Your Horoscope is : Sagittarius'),nl;Ans2==november,Ans1==31,write('It is Imposible, because November just have 30 days !!'),nl;Ans2==december,Ans1<22,write('So, Your Horoscope is : Sagittarius'),nl),!.

















(Click the image if can't see well)

The source code means that everytime user want to use this program, user must entering command "get_answer(Variable1,variabel2)". Variabel1 and variabel2 can change with another variabel name. This program use ARRAY to declare the value of the date and the month of user birthday. For the date, the value of array is from 1 to 31. For the month, the value is from january to december. But february just have 28 day, So we make a condition, if user input 29 for the date of february, the program will show a notification. Like february, if user input 31 for the date of april, june, september, and november, the program also show the notification.

For start the program, entering command get_answer(Variabel1,Variabel2). For example : We use Variabel "Date" and "Month.

Note : variabel must writing a upper case in every first character of name. For example : "Date". Not allowed if you write "date".
















(Click the image if can't see well)

Enter the date of your born (just date, without month and ended by writing dot "." mark). For example : type "7"
















(Click the image if can't see well)

Then, Enter the month of your born (enter the name month in English. For example : april or june. Don't forget to write the dot "." mark in the end of the month). For example : type "april"
















(Click the image if can't see well)

The Result :
















(Click the image if can't see well)

Read more...

Saturday, December 12, 2009

Summary of Chapter 6

Many programming languages provide 'for loops' which enable a set of instructions to be executed a fixed number of times. No such facility is available in Prolog (directly), but a similar effect can be obtained using recursion, as shown in the example programs below.

Example 1

The following program outputs integers from a specified value down to 1.

loop(0).
loop(N):-N>0,write('The value is: '),write(N),nl,
M is N-1,loop(M).

The loop predicate is defined in terms of itself. The second clause can be thought of as: 'to loop from N, first write the value of N, then subtract one to give M, then loop from M'. This process clearly needs to be terminated and this is achieved by the first clause: 'when the argument is zero, do nothing (and hence stop)'. The first clause can be regarded as a terminating condition for the recursion.

?- loop(6).
The value is: 6
The value is: 5
The value is: 4
The value is: 3
The value is: 2
The value is: 1
yes

Example 2

The next program outputs integers from First to Last inclusive.
/* output integers from First to Last inclusive */
output_values(Last,Last):- write(Last),nl,
write('end of example'),nl.
output_values(First,Last):-First=\=Last,write(First),
nl,N is First+1,output_values(N,Last).

Here output_values has two arguments, which can be read as 'output the integers from First to Last inclusive'. The loop terminates when both arguments are the same.

?- output_values(5,12).
5
6
7
8
9
10
11
12
end of example
yes

Looping Until a Condition Is Satisfied

Many languages have an 'until loop' which enables a set of instructions to be
executed repeatedly until a given condition is met. Again, no such facility is
available directly in Prolog, but a similar effect can be obtained in several ways.

Recursion

The first example below shows the use of recursion to read terms entered by the
user from the keyboard and output them to the screen, until end is encountered.

go:-loop(start). /* start is a dummy value used to get the looping process started.*/
loop(end).
loop(X):-X\=end,write('Type end to end'),read(Word),
write('Input was '),write(Word),nl,loop(Word).

?- go.
Type end to end: university.
Input was university
Type end to end: of.
Input was of
Type end to end: portsmouth.
Input was portsmouth
Type end to end: end.
Input was end
yes

Using the 'repeat' Predicate

Although it can often be used to great effect, recursion is not always the easiest way to provide the types of looping required in Prolog programs. Another method that is often used is based on the built-in predicate repeat. The name of this predicate is really a misnomer. The goal repeat does not repeat anything; it merely succeeds whenever it is called. The great value of repeat is that it also succeeds (as many times as necessary) on backtracking. The effect of this, as for any other goal succeeding, is to change the order of evaluating goals from 'right to left' (i.e. backtracking) back to 'left-to-right'. This can be used to create a looping effect, as shown in the examples below. This program repeatedly prompts the user to enter a term until either yes or no is entered. It is an alternative to the recursive program shown at the end of the previous section. In this case it is debatable whether using repeat is an improvement on using recursion, but the example is included for purposes of illustration.

get_answer(Ans):-
write('Enter answer to question'),nl,
repeat,write('answer yes or no'),read(Ans),
valid(Ans),write('Answer is '),write(Ans),nl.
valid(yes). valid(no).

The first five goals in the body of get_answer will always succeed. Evaluating the fifth goal: read(Ans) will prompt the user to enter a term. If the term input is anything but yes or no, say unsure, the following goal valid(Ans) will fail. Prolog will then backtrack over read(Ans) and write('answer yes or no'), both of which are unresatisfiable, i.e. will always fail on backtracking. Backtracking will then reach the predicate repeat and succeed, causing evaluation to proceed forward (left-to-right) again, with write('answer yes or no') and read(Ans) both succeeding, followed by a further evaluation of valid(Ans). Depending on the value of Ans, i.e. the user's input, the valid(Ans) goal will either fail, in which case Prolog will backtrack as far as repeat, as before, or it will succeed in which case the final three goals write('Answer is'), write(Ans) and nl will all succeed. The overall effect is that the two goals write('answer yes or no') and read(Ans) are called repeatedly until the terminating condition valid(Ans) is satisfied, effectively creating a loop between repeat and valid(Ans).

?- get_answer(X).
Enter answer to question
answer yes or no: unsure.
answer yes or no: possibly.
answer yes or no: no.
answer is no
X = no

Goals to the left of repeat in the body of a clause will never be reached on backtracking.

Backtracking with Failure


As the name implies, the predicate fail always fails, whether on 'standard' evaluation left-to-right or on backtracking. Advantage can be taken of this, combined with Prolog's automatic backtracking, to search through the database to find all the clauses with a specified property.

Searching the Prolog Database

Supposing the database contains clauses such as
dog(fido).
dog(fred).
dog(jonathan).

Each dog clause can be processed in turn using the alldogs predicate defined below.

alldogs:-dog(X),write(X),write(' is a dog'),nl,fail.
alldogs.

Calling alldogs will cause dog(X) to be matched with the dog clauses in the database. Initially X will be bound to fido and 'fido is a dog' will be output. The final goal in the first clause of the alldogs predicate will then cause evaluation to fail. Prolog will then backtrack over nl and the two write goals (all of which are unresatisfiable) until it reaches dog(X). This goal will succeed for a second time causing X to be bound to fred.

This process will continue until fido, fred and jonathan have all been output, when evaluation will again fail. This time the call to dog(X) will also fail as there are no further dog clauses in the database. This will cause the first clause for alldogs to fail and Prolog to examine the second clause of alldogs. This will succeed and evaluation will stop.

The effect is to loop through the database finding all possible values of X that satisfy the goal dog(X).

?- alldogs.
fido is a dog
fred is a dog
jonathan is a dog
yes

Note the importance of the second clause of the alldogs predicate. It is there to ensure that, after the database has been searched, the goal succeeds. With only the first line, any call to alldogs will eventually fail.

alldogs:-dog(X),write(X),write(' is a dog'),nl,fail.

?- alldogs.
fido is a dog
fred is a dog
jonathan is a dog
no

Finding Multiple Solutions

Backtracking with failure can also be used to find all the ways of satisfying a goal. Suppose that a predicate findroute(Town1,Town2,Route) finds a route Route between two towns Town1 and Town2. The details of this predicate are irrelevant here. It may be assumed that Town1 and Town2 are atoms and that Route is a list. Backtracking with failure can then be used to find all possible routes between Town1 and Town2 and write out each one on a separate line, as follows:

find_all_routes(Town1,Town2):-
findroute(Town1,Town2,Route),
write('Possible route: '),write(Route),nl,fail.
find_all_routes(_,_).

Read more...

Tuesday, December 8, 2009

Input Output in Prolog (Loop)

Okay, now We will show you three tutorial at this time.

First, We will show you how about to make ouput the values of the squares of the integers from first number to second number inclusive.

Let's make the logic. First, program will receive two inputs from user. First input is number to begin. And the second input is number inclusive for looping. The first number must be multiply with the number itself (because it's squares). After that raise the first number by 1. This prosses will stop if the the first number is larger than the second number. Or we can write like this in mathematic "first number <= second number".

So, we can write the source code like this :

/* Source Code */
squares(Firstnumber,Secondnumber):-Firstnumber>Secondnumber.
squares(Firstnumber,Secondnumber):-write(Firstnumber),write(' squared is '),Square is Firstnumber*Firstnumber,write(Square),nl,A is Firstnumber+1,squares(A,Secondnumber).

The source code means same like the logic. Predicate squares must input by first number and second number. Then the program will check the first number is greater than the second number or not. If no, the program will proceed it by multiply the first number by the same value of the first number (because it's squares). After then, the program will print the first number and the square of the number. And then, the first number will raise by 1 (for loop until second number). Every loop, the program print the number and the square of the number until the number have same value of the second number.

Step 1

Type the source code on the text editor (ex. notepad)
















(Click the image if can't see well)

Step 2

Save it by extention (*.pl) or (*.pro). For example : We named it squares.pro

Note : You must saved it at Directory My Documents\Prolog
















(Click the image if can't see well)

Step 3

Start Prolog and open the source code by command consult.































(Click the image if can't see well)

Step 4

Let's check out the program. For example try the program by type like this on prolog : squares(6,12).

Don't forget to hit Enter.
















(Click the image if can't see well)

Step 5

You can try it by different number by yourself.


This the second tutorial. Now, We will show you how about read in a series of characters input by user and output all of those before the first new line or "?" character.

Let's make the logic. First, will input by charaters. If user hit Enter key, the prolog will start to prosess. The program will show all the character. But, if the program read "?" character, it will stop read the character.

So, you can write the source code like this :
/* Source Code */
begin:-repeat,get0(X),checkchar(X).
checkchar(13):-nl.
checkchar(63):-nl,repeat,get0(X),X=:=13.
checkchar(X):-put(X),fail.

The source code means same like the logic. User must start to use the program by typed "begin". And then, program ready to receive the characters. After user hit Enter, the program will read each of character. If the character is not the "?" character, the program will write it. If the program read the "?" character, the program will ended.

Step 1

Type like this on the text editor (ex. notepad)
















(Click the image if can't see well)

Step 2

Save it by extention (*.pl) or (*.pro). For example : We named it checkchar.pro.

Note : You must save it at directory M Documents\Prolog
















(Click the image if can't see well)

Step 3

Start Prolog and open the source code by command consult.































(Click the image if can't see well)

Step 4

Type like this on Prolog : begin.

Then, hit Enter.

And let's check out the program. For example, type like this on prolog : algorithm

Don't forget to hit Enter.






























































(Click the image if can't see well)

Step 5

Check with the different character with "?" character in it. Type like this : discrete?math

And hit Enter.































(Click the image if can't see well)

Step 6

You can try the another character by yourself.


This is the last tutorial from us right now. In this tutorial, we will show you about how to find a profession person who older than 40 years old in database.

Let's make the logic. The program must have database of the person. And then choose the database "Age" which we want to filter. And then print the database profession from the data which have "Age" more than 40.

So, We can write the source code like this :

/* Source Code */
person(john,smith,45,london,doctor).
person(martin,williams,33,birmingham,teacher).
person(henry,smith,26,manchester,plumber).
person(jane,wilson,62,london,teacher).
person(mary,smith,29,glasgow,surveyor).
findprofession:-person(_,_,Age,_,Profession),Age>40,nl,write('the profession : '),write(Profession),nl,fail.
findprofession.

This source code means same the logic. From the database "person", choose the database "Age". Then filter it which have more than 40 value. After then, print the profession.

Step 1

Type the source code on the text editor (ex. notepad)
















(Click the image if can't see well)

Step 2

Save it by extention (*.pl) or (*.pro). For example : We named it findprofession.pro

Note : You must saved it at directory My Documents\Prolog
















(Click the image if can't see well)

Step 3

Start Prolog and open the source code with command consult































(Click the image if can't see well)

Step 4

Let's check out our the program. Type like this on Prolog : findprofession.

Don't forget to hit Enter.































(Click the image if can't see well)

Read more...

Saturday, December 5, 2009

Input Output in Prolog Part 1 until 5

This is our next Tutorial. This tutorial consist of five Tutorial about Input and Output in Prolog.

First, We'll show about making lower case in Prolog from User's Input. This is the source code :

/* Source Code Lower Case */
lowercase:-get0(A),process(A).
process(13).
process(A):-A=\=13,converter(A,B),put(B),makelower.
converter(A,B):-A>=65,A=<90,b>90.

This is mean if user type "lowercase", Program will ready to receive Input from user. The function of command get0() is to change one or more character to be ASCII Value. Then the ASCII value read by command process. After then, process initialize ASCII value 13 (13 means marriage return or Enter Key). If user type Enter Key, program will check the ASCII value which handled by command process before. If command proccess don't read an "Enter Key" again, Program start function converter which have function to convert ASCII value from 65 (65 is ASCII value of Upper Case A) until 90 (90 is ASCII value of Upper Case Z) by sum it by 32 (Because lower case of A is 97 in ASCII. It's same with another alphabet until Z, just sum it by 32). Then, if in process of convert, program read another character (not alphabet) Program just print it without sum the ASCII value by 32.

Step 1

Type the source code in text editor (ex. notepad).
















(Click the image if you can't see well)

Step 2

Save it at directory My Documents\Prolog and named it by extention (*.pl) or (*.pro). For example named it "lowercase.pro".
















(Click the image if you can't see well)

Step 3

Open it with command consult.































(Click the image if you can't see well)

Step 4

Now, let's check our program. Type "lowercase" (without quotes) and hit Enter.






























(Click the image if you can't see well)

Step 5

Try to type Anything character. For example type like this : "This is an Example 123 inCLUDing numbers and symbols +-*/@[] XYz" (Without quotes) and hit Enter
















(Click the image if you can't see well)

Step 6

Let's try with different input : "ThIs IS jUSt WoRD tO Try ThE PRoGRAm !@#$%^&*()_+" (remember without quotes and hit Enter after type it)
















(Click the image if you can't see well)

This is the second Tutorial. Now, We will show about how to make a program in Prolog which can copy character from a file to another file.

First, let's make a logic from the program. The logic is the program must open the file and check the line per line. In each line, the program must know about the end line and then check the next line. In Prolog, There is command to Open and Write to another file. The commands are see and tell. Another commands are seeing and telling. Seeing and telling must be a variable and will normally be unbound. So, We can write the code like this :

/* Source Code Input from File */
copyterms(Infile,Outfile):-seeing(S),telling(T),see(Infile),tell(Outfile),copy_characters,seen,see(S),told,tell(T).
copy_characters:-read(N),process(N).
process(end_of_file).
process(N):-N\=end_of_file,write(N),write('.'),nl,copy_characters.

This code means that predicate copyterms must be input by file input and file output. Seeing and see in this source code have variable for input, the variable is S. Telling and tell have variable T. The precicate copy_character will read each character and the result of it will be handled by predicate process. Predicate process initialize String "end_of_file". And then, the program will check each character which handled by predicate process. If the character not a String "end_of_file", the program will print it.

Step 1

Type the source code to the text editor (Ex. Notepad)
















(Click the image if you can't see well)

Step 2

Save it at directory My Documents\Prolog. Save it by extention (*.pl) or (*.pro). For example named it "copyterms.pro".
















(Click the image if you can't see well)

Step 3

Make two files (Recommended by extention *.txt) at directory My Documents\Prolog. You must make 2 files. One for Input, and the another to output. For example : named it "input.txt" and "output.txt".
















(Click the image if you can't see well)

Step 4

Start Prolog and open the source code with command consult.
















(Click the image if you can't see well)

Step 5

Now, open the input.txt and type like this :

'first term'. 'second term'.
'third term'.



fourth. 'fifth term'.
sixth.

And then save it.
















(Click the image if you can't see well)

Step 6

Type like this on Prolog : copyterms('input.txt','output.txt').
And hit Enter.
















(Click the image if you can't see well)

Step 7

If Prolog show the notification "Yes", So succesfully copy the character. Now, check your output file and see the characters have been copied successfully.

Note : If your file have any blank line, the program will delete it.































(Click the image if you can't see well)

Step 8

Let's try with different input. Type like this in your input file :

'I am'. just.
check. 'my program'.

'I hope'. this.
is.
can. compiled.
successfully.

And Save it.

Note : You must type '' mark if your input in more than one word. For example : 'I am'. And your each input must ended by stop mark ".".
















(Click the image if you can't see well)

Step 9

Type like this on Prolog again : copyterms('input.txt','output.txt'). And hit Enter.
















(Click the image if you can't see well)

Step 10

If Prolog show the notification "Yes", So succesfully copy the character. Now, check your output file and see the characters have been copied successfully.































(Click the image if you can't see well)

This is the third tutorial from us. We will show about how to read one by one character from a file and convert it to ASCII value on Prolog.

Like usually, let's make the logic of program.

First, program will open a file. And then program will read one by one character. After then program will convert the character to ASCII value and the last print it on screen.

We can write the logic like this :

/* Source Code */
readfile(O):-seeing(S),see(O),readeachcharacter,readeachcharacter,readeachcharacter,readeachcharacter,readeachcharacter,readeachcharacter,readeachcharacter,readeachcharacter,readeachcharacter,readeachcharacter,seen,see(S).
readeachcharacter:-get0(A),write(A),nl.

This source code means that predicate readfile must be input by the input file name. After that program will open the file with predicate see. The predicate readeachcharacter use to convert one by one character which read by predicate readfile to be ASCII value (By predicate get0). Predicate readeachcharacter use to convert A CHARACTER from file, so We must type the predicate readeachcharacter same with the total of character will be converted.

For example, We want to convert ten character from file, So We must type predicate readeachcharacter ten times.

Step 1

Type the logic to text editor (ex. Notepad)
















(Click the image if you can't see well)

Step 2

Save it at directory My Documents\Prolog. Save it by extention (*.pl) or (*.pro). For example named it "readfile.pro".
















(Click the image if you can't see well)

Step 3

Make a file (Recommended by extention *.txt) at directory My Documents\Prolog. For example : named it "testa.txt".
















(Click the image if you can't see well)

Step 4

Start Prolog and open the source code with command consult.































(Click the image if you can't see well)

Step 5

Now, open the input.txt and type like this :

abcde
fghij

And then save it.
















(Click the image if you can't see well)

Step 6

Type like this on Prolog : readfile('testa.txt').
And hit Enter.































(Click the image if you can't see well)

Step 7

You can try with different word. But don't forget to change the total predicate readeachfile if you want to convert more or less character.

This is the fourth tutorial. In this tutorial, We will show about how to combine a file with another using Prolog.

Now, let's make the logic of program.

First, Program will open the first file. Then read each character in it. And then write the same charater in the output file.

Secondly, Program will open the the second file. Then read each character in it. After then write the same character in the output file.

So, We can type like this :

/* Source Code from The Logic */
combine(File1,File2,Out):-seeing(S),telling(T),tell(Out),see(File1),copycharacter,seen,see(File2),copycharacter,seen,see(S),nl,told,telling(T).
copycharacter:-read(N),process(N).
process(end).
process(N):-N\=end,write(N),nl,copycharacter.

This source code means that predicate combine must be input by name of the first file, the second file and the output file. After predicate combine receive the name of files, it will open the first file, read each character and write the same character in the output file. And then open the second file, read each character and write in the output file. On process, if character not identic with String "end", Program will copy it to the output file.

Step 1

Type the logic to text editor (ex. Notepad)
















(Click the image if you can't see well)

Step 2

Save it at directory My Documents\Prolog. Save it by extention (*.pl) or (*.pro). For example named it "combine.pro".
















(Click the image if you can't see well)

Step 3

Make three files (Recommended by extention *.txt) at directory My Documents\Prolog. For example : named it "firstfile.txt", "secondfile.txt" and "output.txt".
















(Click the image if you can't see well)

Step 4

Start Prolog and open the source code with command consult.































(Click the image if you can't see well)

Step 5

Now, open the firstfile.txt and type like this :

just.
try.
the.
program.
end.

And Save it.

Note : Don't forget to type the dot mark "." at every last word and must ended by type "end".
















(Click the image if you can't see well)

Step 6

And then open the secondfile.txt and type like this :

but.
do.
not.
forget.
to.
type.
the.
dot.
mark.
and.
must.
ended.
by.
string-end.
end.

And Save it.
















(Click the image if you can't see well)

Step 7

Now, type like this on Prolog :

combine('firstfile.txt','secondfile.txt','output.txt').
And hit Enter































(Click the image if you can't see well)

Step 8

Open the output file to check the character.
















(Click the image if you can't see well)

Step 9

You can try the different word by yourself.

This the last tutorial from us now. This tutorial is about how to read each character from 2 files and check the character is the same character or not.

Okay, let's make the logic.

First, program will open the first file and the second file and then read the each character.

Secondly, check the character from first file and second file. If the character from the first file and the second file is identic, the program will print the character and give the notification.

So, We can type like this :

/* Source Code from The Logic */
compare(File1,File2):-seeing(S),openandread(File1,File2),see(File1),seen,see(File2),seen,see(S).
openandread(File1,File2):-see(File1),read(X),see(File2),read(Y),(X=Y,write(X),write(' is the same as '),write(Y),nl;X\=Y,write(X),write(' is different from '),write(Y),nl),process(X,Y,File1,File2).
process(end,end,_,_).
process(_,_,File1,File2):-openandread(File1,File2).

This source code means that predicate compare must be input by name of the first file and second file. After the predicate compare receive the name of both files, the program will open and read each character from the files. After then, program will check the character. If the character is similar or identic, program will print the character and give string "as the same as" beetwen it. But if the character not similar or identic, the program will give string "is different from" beetwen it.

Step 1

Type the logic to text editor (ex. Notepad)
















(Click the image if you can't see well)

Step 2

Save it at directory My Documents\Prolog. Save it by extention (*.pl) or (*.pro). For example named it "compare.pro".
















(Click the image if you can't see well)

Step 3

Make two files (Recommended by extention *.txt) at directory My Documents\Prolog. For example : named it "first.txt", "second.txt" and "output.txt".
















(Click the image if you can't see well)

Step 4

Start Prolog and open the source code with command consult.































(Click the image if you can't see well)

Step 5

Now, open the first.txt and type like this :

one.
two.
three.
four.
end.

And Save it.

Note : Don't forget to type the dot mark "." at every last word and must ended by type "end".
















(Click the image if you can't see well)

Step 6

And then open the second.txt and type like this :

one.
second.
three.
fourth.
end.

And Save it.
















(Click the image if you can't see well)

Step 7

Now, type like this on Prolog :

compare('first.txt','second.txt','output.txt').
And hit Enter.































(Click the image if you can't see well)

Step 8

You can try the different word by yourself.

Read more...

About This Blog

This Blog created to complete Discrete Math Assignment. This Blog contain all about discrete math subject especially Prolog programming. The member of team are Fahri Reza, Izzat, Chandra and Catur. We are from Discrete Math class A at Information System Department faculty of Information Technology INSTITUT TEKNOLOGI SEPULUH NOPEMBER (ITS) Surabaya. We hope all existing posts on this blog will be useful for all people. And thanks for visiting our Blog..

Followers

  © Blogger template PingooIgloo by Ourblogtemplates.com 2009 | The Permutation Team's Blog 2009

Back to TOP