Selasa, 24 November 2009

INPUT DAN OUTPUT (makelower)




?- write(9),nl.
9
yes

?- write('Morning Star'),nl.
Morning Star
yes

?- write([widya,riza,dita,sari,[mathematic,on,cyber]),nl.
[widya,riza,dita,sari,[mathematic,on,cyber]]
yes

?- write(mypartners(widya,dita,sari)),nl.
mypartners(widya,dita,sari)
yes

?- write('Morning Star'),nl,nl,write('Bright'),nl.
Morning Star
Bright
yes

?- writeq('Morning Star'),nl.
'Morning Star'
yes

?- read(X).
: riza.
X = riza

?- read(X).
: 9.
X = 9

?- read(X).
: mypartners(widya,dita,sari).
X = mypartners(widya,dita,sari)

?- read(Z).
: [riza,hanum,mypartners(widya,dita,sari),[mathematic,on,cyber]].
Z = [riza,hanum,mypartners(widya,dita,sari),[mathematic,on,cyber]]

?- read(Y).
: 'Morning Star'.
Y = 'Morning Star'

?- X=dita,read(X).
: sari.
no

?- X=riza,read(X).
: riza.
X =riza

INPUT AND OUTPUT (READFILE)

1. Create testa.txt


2. Create in your prolog:




3. The Result Is :

INPUT AND OUTPUT (COPYTERMS)

1. create copyterms.txt






2. Save and now open your prolog, create new rule :




3. Outputfile






Jumat, 13 November 2009

OPERATOR AND ARITHMETIC


Operators and Arithmetics

Operators
Up to now, Prolog user usually use the notation for predicates by a number of arguments in parentheses.
Ex : likes(john,mary)
There is another alternative :


- Two arguments (a binary predicates) be converted to an infix operator ? the functor be written between two arguments with no parentheses
Ex : john likes mary


- One argument (a unary predicate) be converted to :
1. Prefix operator ? the functor be written before the argument with no parentheses
Ex : isa_dog fred
2. Postfix operator ? the functor be written after the argument
Ex : fred isa_dog


Both of predicate (one or two arguments) can be converted to an operator by entering a goal using the op predicate at the system prompt.

Ex : ?-op(150,xfy,likes).
This predicate takes three arguments :
1. 150 (operator precedence) : an integer from 0 upwards
So we can change it with another integer.
2. Xfy : the predicate is binary and is to be converted to an infix operator.
This argument should normally be one of the following three atoms:
1. Xfy
2. Fy : the predicate is unary and is to be converted to an prefix operator
3. Xf : the predicate is unary and is to be converted to a postfix operator
3. Likes : the name of the predicate that is to be converted to an operator. 


Arithmetics
Prolog user can doing arithmetic calculate with prolog, such as :


1. Arithmetic operator
X+Y : sum of X and Y
X-Y : dfference of X and Y
X*Y : product of X and Y
X/Y : quotient of X and Y
X//Y : the ‘integer quotient’ of X and Y (the result is truncated to the nearest integerbetween it and zero)
X^Y : X to the power of Y
-X : negative of X
abs(X) : absolute value of X
sin(X) : sine of X
cos(X) : cosine of X
max(X,Y) : yhe larger of X and Y
sqrt(X) : square root of X


2. Operator presedence in arithmetic expression
Prolog use ordinary algebra algorithm in arithmetic operation.
Ex : A+B*C-D
In the algebra, C*B are calculate first, then the result+A, then the result of sum-D. it is same with those in prolog. But, if we want to calculate A+B, C-D, then multiply both of the result, we must add the parentheses.
Ex : (A+B)*(C-D)


3. Relasion operator
The operator like =,!=, >, >=, <, <= can be used in prolog

Degree operator
Under is the rist of equality operators that used in prolog with the function of each operator:
• Arithmetic Expression Equality ( =:= )
• Arithmetic Expression Inequality ( =\= )
• Terms Identical ( == )
• Terms Not Identical ( \== )
• Terms Identical With Unification ( = )
• Non-Unification Between Two Terms( \= )

Logic operator
a. Operator NOT
Operator not can be placed before predicate to give the negation. Predicate that be negation has the truth value if the origin predicate is false and has the false value if the origin predicate is truth.
The example of using operator not :
dog(fido).
?- not dog(fido).
no
?- dog(fred).
no
?- not dog(fred).
Yes


b. Disjunction operator
Operator disjungsi (;) digunakan sebagai operator ‘atau’. Contoh :
?- 6 > 3;7 is 5+2.
yes
?- 6*6=:=36;10=8+3.
yes



Figure out the Practical Exercise 4 Page : 68