Selasa, 08 Desember 2009

Resume bab 6


RESUME
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
This chapter 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'.



Tidak ada komentar:

Posting Komentar