PPAI/L2
From WikiZMSI
< PPAI
Spis treści |
[edytuj]
Part 2 - Data objects, operators, matching
[edytuj]
Data objects, operators
- Three types of data objects: atoms and numbers, variables, structures
- Operator notation:
- +
- -
- /
- // integer division
- *
- ** power
- mod modulo
- is arithmetic equal np. ( X is 1 mod 3. )
- =:= comparing if two values are equal
- =\= comparing if two values are not equal
- >
- <
- >=
- =<
- Task 1: Check on Family.pro:
parent(X,_). mother(_,_). mother(X,X).
- Task 2: Define the relation hasachild/1 and isgrandparent/0
- Task 3: Define the relation that gives the distance between two points in two and three dimensional space. Use: p2(X,Y) and p3(X,Y,Z) structure.
distance(p2( X1, Y1), p2(X2, Y2), D) :- D is ((X2-X1)**2 + (Y2-Y1)**2))**(1/2).
distance(p3( X1, Y1, Z1), p3(X2, Y2, Z2), D) :- D is ????.
[edytuj]
Matching
- Task 1: Try each command in the question mode. What is the meaning?:
X = alice.
X = Y.
alice = malice.
X = date(1, may, 2014).
date(D, may, 2013) = date(1, may, 2014).
date(D, may, 2013) = date(1, may, R).
fun(X) = fun(fun(X)).
3 = 3.
1+2 = 3.
1+2 =:= 3.
alice == alice.
X == Y.
X == X.
2+1 == 1+2.
alice \== malice.
horizontal( p( 1, 2), p( A)) = horizontal( B, p( 1, 2)).
horizontal( p( 1, 2), p( A,B)) = horizontal( B, p( 1, C)).
[edytuj]
Exercises
- Ex 1: Define the relation max(X,Y,Max) so that Max is the greater of two numbers X and Y.
- Ex 2:
- Ex 3: Define the relation gcd/3. For example gcd(5,10,5) is satisfied. Use the three following rules:
- If X and Y are equal then D is equal to X
- If X <Y than D is equal to the greatest common divisor of X and the Difference Y-X
- If Y<X is the same as in case (2) with X and Y interchanged.
- Ex 4: Look at and consult the prolog source code sentence.pro
- How to ask questions to this code?
- What is the order of generated sentences.
[edytuj]
To do
1. Reading: chapter 2 "Syntax and Meaning of Prolog Programs", chapter 3.3 "Operator notation" and 3.4 "Arithmetic"
[edytuj]