PPAI/L2

From WikiZMSI

Spis treści

Part 2 - Data objects, operators, matching

Data objects, operators

  1. Three types of data objects: atoms and numbers, variables, structures
  2. 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
    • >
    • <
    • >=
    • =<
  3. Task 1: Check on Family.pro:
parent(X,_).  
mother(_,_). 
mother(X,X).
  1. Task 2: Define the relation hasachild/1 and isgrandparent/0
  2. 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 ????.

Matching

  1. 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)).

Exercises

  1. Ex 1: Define the relation max(X,Y,Max) so that Max is the greater of two numbers X and Y.
  2. Ex 2:
  3. Ex 3: Define the relation gcd/3. For example gcd(5,10,5) is satisfied. Use the three following rules:
    1. If X and Y are equal then D is equal to X
    2. If X <Y than D is equal to the greatest common divisor of X and the Difference Y-X
    3. If Y<X is the same as in case (2) with X and Y interchanged.
  4. Ex 4: Look at and consult the prolog source code sentence.pro
    1. How to ask questions to this code?
    2. What is the order of generated sentences.

To do

1. Reading: chapter 2 "Syntax and Meaning of Prolog Programs", chapter 3.3 "Operator notation" and 3.4 "Arithmetic"

Recursion

  1. slides