Wednesday, December 16, 2009
Tuesday, December 8, 2009
Practical Excercise Bab 6
to output squares of two integers, we can use the goal 'outsquare'. Just enter the inputs you want, and it will be the range.
The example of the form :
This is the example of the program :
2. Define and testing predicate
this program using the repeat predicate. in this part, we use goal 'go'. This is the form :
after you consult the file, enter the goal 'go'. then, you need to enter the input you want. here is the output on SWI-PROLOG
3. Backtracking with failure
This followed program using 'backtracking with failure'. We will appearing the person with age above 40. Similiar with the former chapter on prolog, which use the 'find' function. Enter the facts that needed here, like first name, last name, age, city, and profession. Seems like this example :
Then, use the goal 'find'. Here's the output :
Chapter 6 from E-Book : Looping with PROLOG
Loops enable a set of instructions to be executed a fixed number of times. In prolog, looping can be obtained using recursion. In this case, looping function on PROLOG is similiar to another programming language. We can see in the example given below:
loop(0).
loop(N):-N>0,write('The value is: '),write(N),nl,
M is N-1,loop(M).
Those clauses means that ‘the terminating condition of the recurtion is 0. To loop from N, first determine the value of N, then substract it to having M, then loop from M, until the value is 0. If the value is 0, the looping process is stopped there’
6.2 LOOPING UNTIL A CONDITION IS SATISFIED
No facility on PROLOG that directly enable a set of instructions to be executed repeatedly until a given condition is met. But there are two way to obtained the similiar effect :
- Recursion
Recursion read the input from keyboard, and output it to the screen, until the ‘end’ instruction is endountered.
Example of the form :
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).
The last clauses mean: The looping of X will be stopped if the ‘end’ is encountered. Saat looping berlangsung (before ‘end’ had encountered), PROLOG will always ask the user to enter the input by the sentence ‘Type end to end’ .
- Using the ‘repeat’ Predicate
The goal repeat doesn’t repeat anything, it will be succeeds whenever it’s being called. If user enter another term but yes or no, PROLOG will always repeat the instruction until the user enter the term ‘yes’ or ‘no’. This is the example of the form :
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).
In the case of looping, the two goals write(‘answer yes or no’) and read(Ans) will always repeat until the condition valid(Ans) had satisfied.
Repeat predicate can also processing the sequence of terms from a specified file an outputs them until the term ‘end’ is encountered.
The example of the form :
readterms(Infile):-
seeing(S),see(Infile),
repeat,read(X),write(X),nl,X=end,
seen,see(user).
Then, the file that will being outputted containing (for example, the file ‘myfile.txt’ :
'first term'. 'second term'.
'third term'. 'fourth term'.
'fifth term'. 'sixth term'.
'seventh term'.
'eighth term'.
end.
Then, call the goal readterms will produced a result :
?- readterms('myfile.txt').
first term
second term
third term
fourth term
fifth term
sixth term
seventh term
eighth term
end
yes
If ‘end’ hasn’t been encountered, there will be a looping process between the goals repeat and X=end.
Thursday, November 26, 2009
excercise prolog chapter 5 (cont'd)
in order that the output file contain the terms in the first input file followed by the terms in the second, you must write some rules using predicate combine in notepad like the picture below:
After you write the program, you can consult it on SWI PROLOG then you can see how’s the output, like we can see below:
5. in order that we can reads that two text files term by term and each pair of corresponding terms output a messsage saying that they are the same or different, we must open two files that contain the same starting term and finished by end. Like we can see below this :
after that, we need to make a formula on notepad using predicare ‘compare’. Then, consult it on SWI PROLOG.
this is it, the output:
exercise prolog chapter 5
1.Changing from Upper to Lower
- Make a predicate make lower in notepad and writing formulas to be written uppercase to lowercase. Example:
=X<=90,huruf besar ada dalam ambang 65-90(menurut ASCII).">Numbers 65> = X <= 90, upper threshold of 65-90 in (according to ASCII). Figures 32 to print a space.
- Saved in. Pl
- Opening prologue and consult
- =X<=90,huruf besar ada dalam ambang 65-90(menurut ASCII).">Numbers 65> = X <= 90, upper threshold of 65-90 in (according to ASCII). Figures 32 to print a space.
- Saved in. Pl
- Opening prologue and ending consultketik makelower dot (.)
- Write the words you want capitalized, for example: Denia, RIFAnda, QORY, and Deby and ..
- The results:
2. Question
Display prologue
3. Creating readfile predicate in the notepad, as follows:
- Saved in. Pl
- Opening prologue and consult
- Type readfile ending point (.)
- Write ABCDE fghij
- The results:
Wednesday, November 25, 2009
Chapter 5 form E-book : Logic Programming Using PROLOG
5.1 Outputting terms
The write/1 predicate takes a single argument, which must be a valid Prolog
term. The built-in predicate nl/0 has also been used many times previously. It takes no arguments. Evaluating a nl goal causes a new line to be output to
the current output stream.
Example:
?- write(college),nl.
college
Yes
5.2 Inputting term
The built-in predicate read/1 is provided to input terms. It takes a single argument,
which must be a variable.
In the input stream, the term must be followed by a dot ('.') and at least one white space character, such as space or newline. The dot and white space characters are read in but are not considered part of the term.
If the argument variable is already bound the goal succeeds if and only if the
input term is identical to the previously bound value.
?- X=rooney,read(X).
: rooney.
X = rooney
5.3 Input and Output using computer
Although input and output of terms is easy, the use of quotes and full
stops can be not always suitable.
A much better approach for
problems of this kind is to input a character at a time. To do this it is first necessary
to know about the ASCII value of a character..
All printing characters and many non-printing characters (such as space and
tab) have a corresponding ASCII (American Standard Code for Information
- 1. The predicate readin is defined recursively. It causes a single character to be
Prologue to take input from the current input stream and take the output from the current output stream. For example, the keyboard as input devices and monitors as a means of output. Input and output can also be raised through files and files. Users can open and close the input and output with a variety of files, but can only process one type of file it at the same time.
5.8 File Output : Changing the Current Output Stream
Predicate tell / 1
Current output stream can be changed using this Predicate. With this Predicate, the file was not opened will be deleted and replaced with the current output stream.
Predicate told / 0
This Predicate causes the file name changed to the user's terminal.
Predicate telling / 1
This Predicate causes the variable bound by the name of the current output stream.
5.9 File Input: Changing the Current Input Stream
Current input stream can be changed using the Predicate see, followed by the atom, as an example, see ( 'myfile.txt'). This Predicate causes the file name changed to current input stream. see the Predicate consists of several parts. First, the order seen / 0 which causes current input file into the user's terminal.
Reading from Files: End of File
If the end of the file found when evaluating Predicate get (X) or get0 (X), variable X will be bound to specific numerical values. If the range is 0-255 ASCII code, the code that appears before the code is 1.
Reading from Files: End of Record
End of the line indicated by karakterASCII input value 13. End of the record indicated by 2 values of 13 ASCII characters followed by 10.
Tuesday, November 24, 2009
Simple Expert System Idea
As we know, the expert system is a system that tried to adopt human knowledge to computer designed to model the ability to solve problems like an expert. In this case, we apply the expert system to identify the race of people in the world.
In the world of so many human beings from different types of countries. So that we do not tell people where it came from, then we have to identify their characteristics. By identifying them we will also gain some advantages.
* Mongoloid, with the characteristics:
yellow to dark brown,
straight hair,
little body hair,
narrow eyes especially Asian Mongoloids
smaller and shorter than Kaukasoid race.
North Asia, East Asia, Southeast Asia, North Europe, North America, South America
* Kaukasoid, has the physical characteristics
nose,
whites,
Wavy hair
blonde hair brown to blackish,
straight eyelid
Europe, North Africa, Middle East, Pakistan and North India
* Negroid, with physical characteristics
curly hair,
blacks,
high body
thick lips
straight eyelid
Africa, Guinea, Melanesia
* Austroloid, including indigenous Australians (Aboriginal)
Black
curly black hair
southern India, Sri Lanka, the Original in Malaysia
Benefit:
1. Helping the customs that no illegal immigrants
2. For general knowledge about human diversity in the world
3. No racism