Prolog Notes
Prolog Notes
Is:
---
<Variable> is <expression>
N is 6+1 -> N =7
<Constant> is <expression>
7 is 6+1 -> true
equal(=):
---------
N = 6+1 -> N = 6+1 (take it as string with no evaluation)
(=:=):
------
N =:= 6 -> error (doesnt take variable)
(\=):
-----
4 \= 4. -> false (checks if 4 is unequal to 4)
4 \= 2+2. -> true (doesnt evaluate term on right and it takes it as string)
X \= 4. -> false (doesnt know what X is so it return false as fails to know X)
X \== 4. -> true (can differ bet variable and constant and return they are not
the same)
(=\=):
------
4 \= 2+2. -> false (evaluate term on right)
(==):
-----
X == 2. -> false (matches both)
2 == 2. -> true
---- You cant make a query of (2+3), you need to put it in a variable to return
result like (x is 2+3)
Operators:
---------
// integer division -> 5//3
/ double division (result is decimal) -> 5/3
mod -> 5 mod 3
abs() -> abs(-3)
float() -> float(19) -> 19.0
round() -> round(5.7) -> 6 ()تقريب
ceiling() -> ceiling(5.1) -> 6
floor() -> floor(5.7) -> 5 (floor(-5.1) -> -6)
truncate() -> truncate(5.9) -> 5
*Queries Examples:
------------------
?- parent(X, Y), X \= ali. % means give me those relations when parent is not
ali
X = nour,
Y = amr;
X = amr,
Y = camilia;
X = amr,
Y = omar;
trace:
-----
?- trace.
predecessor(X, Y):-
parent(X, Z),
predecessor(Z, Y).
Recursion:
---------
Head Recursion(call then arithmatic operation):
-----------------------------------------------
sumToN(1,1).
sumToN(N,R):-
N1 is N-1,
sumToN(N1,R1),
R is N+R1.
sumToN(N,Temp,R):-
N1 is N-1,
NewTemp is N+Temp,
sumToN(N1,NewTemp,R).
powerr(X,Y,R):-
Y >0,
Y1 is Y-1,
powerr(X,Y1,R1),
R is X*R1.
powerr(X, Y, Temp, R) :-
Y > 0,
Y1 is Y - 1,
NewTemp is Temp * X,
powerr(X, Y1, NewTemp, R).
Factorial(Head Version):
------------------------
factorial(1,1).
factorial(X,R):-
X > 1,
X1 is X-1,
factorial(X1,R1),
R is X*R1.
Factorial(Tail Version):
------------------------
factorial(1,R,R).
factorial(X,Temp,R):-
X > 1,
X1 is X-1,
NewTemp is X*Temp,
factorial(X1,NewTemp,R).
Module:
-------
to link two files
In first file:
-------------
module(name_module, [rule1/no_arguments , rule2/no_arguments]).
rule2(argument3, Result2):-
......
In second file:
--------------
use_module(name_module).
--if u consult 2nd file and run predicate go, it will call other predicates from
file1.
-----------------------------------------------------------------------------------
-------
List:
-----
N = [dog, 1, 2, X, Parent(X, Ali)]
or N = [] (empty)
[[a,b]|[c]] = [[a,b],c]
member function:
---------------
member(a, [a, b, c]). -> true.
member([a], [a, b|[c]]). -> false. (a is member while [a] is not member)
lastElement([H],H).
lastElement([H|T],L):-
lastElement(T,L).
beforeLast([H,S],H).
beforeLast([H|T],L):-
beforeLast(T,L).
length([],0).
length([H|T], R):-
length(T,R1),
R is R1+1.
example:
length([a, b,[c, d],e], N). -> N = 4.
suffix (using append): (first list appears at the end of second list)
----------------------
suffix(List1, List2):-
append(_, Last1, List2).
example:
suffix([3,4,5],[1,2,3,4,5]). -> true.
prefix (using append): (first list appears at the beginning of second list)
---------------------
prefix(List1, List2):-
append(Last1, _, List2).
example:
prefix([1,2,3],[1,2,3,4,5]). -> true.
example:
last([5,[1,2,3,4,5]). -> true.
addone([First|Rest], [Add|Rest1]):-
Add is First+1,
addone(Rest,Rest1).
example:
nth(X,[x,3,5,9,10],4). -> X = 10.