Daftar Blog Saya

Selasa, 15 Desember 2009

FP 6

(1) Define a predicate to output the values of the squares of the integers from N1 to
N2 inclusive and test it with N1 = 6 and N2 = 12.
Here is the answer :
first open your notepad and type this code

then save as loop coba4.pl ( I name it like that because before I done it, I did some experiments)
load it in prolog and here is the result

it is so simple.
(2) Define and test a predicate to read in a series of characters input by the user and output all of those before the first new line or ? character.
this one is really the hardest of all, but thanks to some of  our friends we fnally figure it out
again, open your notepad and type this code :
save it. and load it in the prolog. here is the result

and finally number three
(3) Using the
person clauses given in Section 6.3.1, find the professions of all those over 40.
here is the source code :

and here is the result :

I’m sorry i didn’t included any explanations because i’m (the one who upload it) very tired already.  there is so many things to be done today. and tomorrow the hell is not over yet…. hahaha
and for my lecturer Mr. AM. I’m really sorry for being late to post this, it’s because there is so many things to do this day sir. I hope you could understand. thanks for reading this note.

INPUT AND OUTPUT

Chapter 6 : Loops
             This chapter discusses about how to obtain the effect of a loop in prolog. Because there is no such a built-in operator that can directly gives us the effect of loop. But there is such a way to obtain it. Here we go
Here is the explanation of th subchapters in the chapter 6, including the examples:
Subchapter 1 : Looping a Fixed Number of Times
            Explain how to obtain the effect of loop in prolog
example 1 :
The following program outputs integers from a specified value down to 1.
loop(0).loop(N):-N>0,write(‘The value is: ‘),write(N),nl, M is N-1,loop(M).
 The loop predicate is defined in terms of itself. The second clause can be thought of as: ‘to loop from N, first write the value of N, then subtract one to give M, then loop from M’. This process clearly needs to be terminated and this is achieved by the first clause: ‘when the argument is zero, do nothing (and hence stop)’. The first clause can be regarded as a terminating condition for the recursion. Here is the result:
?- loop(6).
The value is: 6
The value is: 5
The value is: 4
The value is: 3
The value is: 2
The value is: 1
yes
example 2 :
The next program outputs integers from First to Last inclusive.
/* output integers from First to Last inclusive */output_values(Last,Last):- write(Last),nl, write(‘end of example’),nl.
output_values(First,Last):-First=\=Last,write(First),
nl,N is First+1,output_values(N,Last).
Output :
?- output_values(5,12).
56789
10
11
12
end of example
example 3 :
Define a predicate to find the sum of the integers from 1 to N (say for N = 100).
/* sum the integers from 1 to N (the first argument)inclusive */ sumto(1,1).
sumto(N,S):-N>1,N1 is N-1,sumto(N1,S1),S is S1+N.
 Output :
?- sumto(100,N).
N = 5050
?- sumto(1,1).
yes
example 4 :
Define a predicate to output the squares of the first N integers, one per line.
/* output the first N squares, one per line */writesquares(1):-write(1),nl. writesquares(N):-N>1,N1 is N-1,writesquares(N1),
Nsq is N*N,write(Nsq),nl.
?- writesquares(6).
149
16
25
36
yes
subchapter 2 : Looping Until a Condition Is Satisfied
2.1. Recursive
example :
the use of recursion to read terms entered by the user from the keyboard and output them to the screen, until end is encountered.
go:-loop(start). /* start is a dummy value used to getthe looping process started.*/ loop(end).
loop(X):-X\=end,write(‘Type end to end’),read(Word),
write(‘Input was ‘),write(Word),nl,loop(Word).
 or
loop:-write(‘Type end to end’),read(Word),write(‘Input was ‘),write(Word),nl, (Word=end;loop).
The output is the same :
?- loop.
Type end to end: university.
Input was university
Type end to end: end.
Input was end
yes
2.2. Using the ‘repeat’ Predicate
repeat can be used to shorten the program
before:
get_answer(Ans):-write(‘Enter answer to question’),nl,get_answer2(Ans). get_answer2(Ans):-
write(‘answer yes or no’),
read(A),
((valid(A),Ans=A,write(‘Answer is ‘),
write(A),nl);get_answer2(Ans)).
valid(yes). valid(no).
 after
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).
the output is the same.
Subchapter 3 : Backtracking with Failure
            How to use the advantage of the predicate fail/0
3.1. searching the prolog database
dog(fido).dog(fred). dog(jonathan).
alldogs:-dog(X),write(X),write(‘ is a dog’),nl,fail.
alldogs.
 after the alldogs predicate has succeded, the fail/0 predicate will cause it to execute the predicate alldogs until the result is no. then the alldogs/0 predicate will cause the last line is is Yes not No because it considered as succeeded. Here is the output :
?- alldogs.
fido is a dog
fred is a dog
jonathan is a dog
yes
3.2. Finding Multiple Solutions
Backtracking with failure can also be used to find all the ways of satisfying a goal.
Example :
find_all_routes(Town1,Town2):-findroute(Town1,Town2,Route), write(‘Possible route: ‘),write(Route),nl,fail.
find_all_routes(_,_).
  I’m sorry, but the output was not listed.

Input and Output (summary)

Input and Output

1. Outputting Terms
The main built-in predicate provided for outputting terms is write/1, The write/1 predicate takes a single argument, which must be a valid Prolog term. Evaluating the predicate causes the term to be written to the current output stream, which by default is the user’s screen. Another built-in predicate nl/0 which takes no arguments. Evaluating a nl goal causes a new line to be output to the current output stream. Example:
?- write(26),nl.
26
Yes

2. Inputting Terms
The built-in predicate read/1 is provided to input terms. It takes a single argument, which must be a variable. Evaluating it causes the next term to be read from the current input stream, which by default is the user’s keyboard. 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. When a read goal is evaluated, the input term is unified with the argument variable. If the variable is unbound (which is usually the case) it is bound to the input value.
?- read(X).
: jim.
X = jim
If the argument variable is already bound (which for most users is far more likely to occur by mistake than by design), the goal succeeds if and only if the input term is identical to the previously bound value.
?- X=fred,read(X).
: fred.
X = fred

3. Input and Output Using Characters
The use of quotes and full stops can be cumbersome and is not always suitable. For example, it would be tedious to define a predicate (using read) which would read a series of characters from the keyboard and count the number of vowels. 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. Here is the table :

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, for example :
?- put(64),nl.
@
Yes

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. The variable is then unified with the ASCII value of this character.
Assuming the argument variable is unbound (which will usually be the case), it is bound to the ASCII value of the input character.
?- get0(N).
: a
N = 97
The get predicate takes a single argument, which must be a variable. Evaluating a get goal causes the next non-white-space character (i.e. character with an ASCII value less than or equal to 32) to be read from the current input stream. The variable is then unified with the ASCII value of this character in the same way as for get0.
?- get(X).
: Z
X = 90

6. Using Characters: Examples

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 * 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. This process goes on indefinitely until a * character is read. (In the example below, the ASCII values of characters P, r, o etc. are correctly shown to be 80, 114, 111 etc.)
readin:-get0(X),process(X).
process(42).
process(X):-X=\=42,write(X),nl,readin.


7. Input and Output Using Files
Prolog takes all input from the current input stream and writes all output to the current output stream. By default both of these are the stream named user, denoting the user’s terminal, i.e. keyboard for input and screen for output. The same facilities available for input and output from and to the user’s terminal either term by term or character by character are also available for input and output from and to files (e.g. files on a hard disk or a CD-ROM). The user may open and close input and output streams associated with any number of named files but there can only be one current input stream and one current output stream at any time. Note that no file can be open for both input and output at the same time (except user) and that the user input and output streams cannot be closed.

8. File Output: Changing the Current Output Stream
The current output stream can be changed using the tell/1 predicate. This takes a single argument, which  is an atom or variable representing a file name, e.g. tell(‘outfile.txt’). Evaluating a tell goal causes the named file to become the current output stream. If the file is not already open, a file with the specified name is first created (any existing file with the same name is deleted). The default current output stream is user, i.e. the  user’s terminal. This value can be restored either by using the told predicate or by tell(user). The built-in predicate told/0 takes no arguments. Evaluating a told goal causes the current output file to be closed and the current output stream to be reset to user, i.e. the user’s terminal. The built-in predicate telling/1 takes one argument, which must be a variable and will normally be unbound. Evaluating a telling goal causes the variable to be bound to the name of the current output stream.

9. File Input: Changing the Current Input Stream
current input stream can be changed using the see/1 predicate. This takes a single argument, which is an atom or variable representing a file name, e.g. see(‘myfile.txt’). Evaluating a see goal causes the named file to become the current input stream. If the file is not already open it is first opened (for read access only). If it is not  possible to open a file with the given name, an error will be generated. The default current input stream is user, i.e. the user’s terminal. This value can be restored either by using the seen predicate or by see(user). The built-in predicate seen/0 takes no arguments. Evaluating a see goal causes the current input file to be closed and the current input stream to be reset to user, i.e. the user’s terminal. The built-in predicate seeing/1 takes  one argument, which must be a variable and will normally be unbound. Evaluating a seeing goal causes the variable to be bound to the name of the current input stream.

Innovative Drinking Machine

Latar belakang:
Minuman adalah suatu yang mutlak di perlukan oleh seseorang dalam keadaan haus ataupun juga saat bersantai melepas penat dan bahkan menyembuhkan suatu penyakit. Minuman memberikan kesan tersendiri saat seeorang meminumnya, contohnya: saja coklat panas memberikan sensasi rileks pada seseorang yang meminumnya. Saat seseorang pergi ke toko banyak pilihan yang ditawarkan, tetapi belum tentu minuman tersebut memberikan sensasi yang sesuai yang dibutuhkan tubuh kita dan lingkungan kita. Oleh karena itu kami terpikirkan suatu aplikasi yang dapa membantu memberikan pilihan minuman yang memberikan sensasi yang sesuai dengan kebutuhan kita. Aplikasi ini juga dapat memberikan saran berupa obat kepada seseorang yang memberikan inputan kata yang mengindikasikan seseorang tersebut dalam keadaan sakit.
Penjelasan ide:
Mesin minuman ini mengusung aplikasi prolog di dalam pengoprasiannya. Didalam mesin ini dimasukkan bermacam macam jenis minuman dan beberapa obat. User menginputkan kondisi yang di terdapat dalam kotak dialog sehingga mesin dapat menyarankan minuman yang sesuai dengan kebutuhan. Contoh kotak dialog tersebut adalah: “Suhu lingkungan; pekerjaan yang baru saja dilakukan; dan kondisi badan”.
Manfaat:
-Mempermudah dalam memperoleh minuman yang dibutuhkan tubuh.
-Efisien dan terjangkau Karena mesin ini ada di tempat yang di butuhkan
-Menghemat waktu dalam memilih minuman.
Kesimpulan:
Jadi mesin minuman ini akan memudahkan seseorang mengembalikan kondisi tubuhnya pada keadaan yang optimal dan diinginka. Sehingga nesin ini dapat membaantu dalam mengoptimalkan hari-hari seseorang.

Tidak ada komentar:

Posting Komentar