Wednesday, December 16, 2009

Tuesday, December 8, 2009

Practical Excercise Bab 6

1. Squares of integers.

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

6.1 LOOPING A FIXED NUMBER OF TIMES

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.

6.3 Backtracking with Failure

ex:
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.

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.

The next program is designed to search a database containing clauses
representing the name, age, place of residence and occupation of a number of
people.It should be noted that it is not always necessary to use 'backtracking with
failure' to search the database.


Finding Multiple Solutions

Suppose that a predicate findroute(Town1,Town2,Route) finds a route Route
between two towns Town1 and Town2.

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


So, Backtracking with failure can also be used to find all the ways of satisfying a goal.
And also describes how a set of goals can be evaluated repeatedly in Prolog,
either a fixed number of times or until a specified condition is met, and how
multiple solutions can be arrived at using the technique of 'backtracking with
failure'.



Thursday, November 26, 2009

excercise prolog chapter 5 (cont'd)

4. Create the text that you want to combine on notepad, for example: the file ‘ntar.txt’ and ‘ntar2.txt’ that we can see below:



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


The formula used...

Display prologue

Output



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

Interchange) value, which is an integer from 0 to 255.





5.4 Outputting Characters


Characters are output using the built-in predicate put/1. The predicate takes a
single argument, which must be a number from 0 to 255 or an expression that
evaluates to an integer in that range.
Evaluating a put goal causes a single character to be output to the current
output stream. This is the character corresponding to the numerical value (ASCII
value) of its argument.

EXAMPLE :
?- put(97),nl.
a
yes


5.5 Inputting Characters

Two built-in predicates are provided to input a single character: get0/1 and get/1.
The get0 predicate takes a single argument, which must be a variable. Evaluating a
get0 goal causes a character to be read from the current input stream.


Assuming the argument variable is unbound :
?- get0(N).
: Z
N = 90

If the argument variable is already bound,:

?- get0(X).
: a
X = 97

?- M is 41,get0(M).
: )
M = 41


The get predicate takes a single argument, which must be a variable.
The
variable is then unified with the ASCII value of this character in the same way as
for get0.

?- get(X).
: Z
X = 90


5.6 Using Characters


  1. 1. The predicate readin is defined recursively. It causes a single character to be
input and variable X to be bound to its (numerical) ASCII value. The action taken
(the process(X) goal) depends on whether or not X has the value 42 signifying a *
Input and Output 75
character. If it has, the evaluation of the goal stops. If not, the value of X is output,
followed by a new line, followed by a further call to readin.

2.
ASCII
values of the input characters are not output, but the number of characters
(excluding the *) is output. The count predicate is defined with two arguments
which can be read as 'the number of characters counted so far' and 'the total number
of characters before the *

3.
count predicate can be interpreted as 'the
number of vowels so far' and 'the total number of vowels'.
The three arguments of
the process predicate can be read as 'the ASCII value of an input character', 'the
number of vowels up to but not including that character' and 'the total number of
vowels', respectively.



5.7 Input and Output Using Files

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

Background
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