Sabtu, 05 Desember 2009

LOOPS

Most conventional programming languages have a looping facility that enables a set of instructions to be executed repeatedly either a fixed number of times or until a given condition is met. Prolog has no looping facilities, similar effects can be obtained that enable a sequence of goals to be evaluated repeatedly. This can be done in a variety of ways, using backtracking, recursion, built-in predicates, or a combination of these.

1.1 Looping a Fixed Number of Times

No such facility loop in prolog, but a similar effect can be obtained using recursion.

Ex :

1. Make a new rule notepad.


2. then save .pl

3. and then open prolog consult that file and write an instruction ?- loop(6). And then push enter .

4. and the result is

Ex 2 :

1. make a rule in notepad.


2. then save .pl

3. and consult in prolog .

4. and then write a instruction output_values(4,10). And the result is :


1.2Looping Until a Condition Is Satisfied

No such facility a set of instructions to be executed repeatedly until a given condition is met available directly in Prolog, but a similar effect can be obtained in several ways.

1.2.1 Recursion

Ex1 :

1. Make a new rule in notepad.


2. The second save .pl

3. And then open in prolog and consult the file .

4. Write a instruction go. Then push enter , and then write the the answer (without spasi and don’t forget to give (.) ).

5. The result is :


Ex 2 :

1. Make a rule in notepad .


2. Second step is you must ave it .pl

3. And then open prolog , consult the file .

4. In prolog you must write an instruction get_answer(Myanswer).

5. The result is :


1.2.2 Using the ‘repeat’ predicate.

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.

Ex1 :

1. First make a rule in notepad .


2. Second save it with format .pl

3. Then consult this file in prolog .

4. Write the instruction get_answer(Y). then push enter .

5. Show the evaluation will prompt the user to enter a term.

6. Answer it with yes or no, it can’t stop until you aswer Yes or No.


Ex 2 :

1. Make a rule in notepad then save .pl


2. Then make a new file with name myfile.txt


3. Open prolog and consult the file .

4. And then write an instruction readterms(‘myfile.txt’). please don’t forget to bring (.).

5. The result is :


1.3Backtracking with Failure

1.3.1 Searching the Prolog Database

Ex 1 :

1. Make a rule in notepad and save .pl

2. Then consult the file in prolog .

3. And then write alldogs. in prolog

4. And the result is :


5. But if in rule you write this


6. The result is :


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.

Ex 2 :

1. Make a rule in notepad and then save .pl


2. Second open the prolog and consult the file

3. Write allteachers. In prolog

4. And the result is .

1.3.2 Finding Multiple Solutions

Backtracking with failure can also be used to find all the ways of satisfying a goal.

Tidak ada komentar:

Posting Komentar