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)

0 comments:

Post a Comment

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