أخر الاخبار

Examples for the prolog(Knowledge Representation)

Examples for the prolog

Example1: Write a prolog program to check if the given number is positive or negative.

sol//

Domains I= integer 

Predicates pos_neg(i) 

Clauses pos_neg(X):-X>=0, write(“positive number”),nl. 

 pos_neg(_ ):-write(“negative number”),nl. 

Goal pos_neg(4). 

Output:

positive number


Example2: write a prolog program to check if a given number is odd or even.

sol//

Predicates 

 odd_even(integer) 

Clauses 

 odd_even(X):-X mod 2= 0, write (“even number”), nl.

 odd_even(X):- X mod 2 <> 0, write (“odd number”), nl. 

Goal odd_even(5). 

Output: 

 odd number

Example3: Write a prolog program to combine both rules in example1 and example2.

sol//

Domains 

 I= integer

 Predicates 

 pos_neg(i) 

 odd_even(i)

 oe_pn(i) 

Clauses

 oe_pn(X):-pos_neg(X),odd_even(X).

 odd_even(X):-X mod 2= 0, write(“ even number”),nl. 

 odd_even(X):- write(“odd number”),nl.

 pos_neg(X):-X>=0, write(“positive number”),nl.

 pos_neg(_ ):-write(“negative number”),nl.

 Goal oe_pn(3). 

Output: 

 odd number

 positive number

Example4: write a prolog program that take two integers as input and print the greater one.

sol//

Domains

 i = integer 

Predicates 

 greater ( i,i) 

Clauses 

 greater(X,Y):- X>Y,write(“the greater is”,X). 

 Greater(X,Y):- Y>X, write (“ the greater is “,Y). 

Goal Greater(4,3). 

Output: 

 The greater is 4

Example5: program to print number from n to 1. 

sol//

Predicates

 a (integer) 

Clauses 

 a(1) :- write (1), nl ,!. 

 a(M):- write (M) , nl, M1 = M -1, a( M1). 

Goal a(4).

 Output: 

Yes

Example 6: program to find factorial. 5! = 5*4*3*2*1 

sol//

Predicates 

 fact ( integer, integer, integer)

 Clauses

 fact(1, F, F):-!. 

 fact(N,F,R):- F1=F*N , N1=N-1, fact(N1,F1,R). 

Goal fact (5,1,F).

 Output: 

 F = 120. 

Example 7: program to find power . 3 4 = 3*3*3*3.

sol//

Domains

 I= integer

 Predicates 

 power ( I,I,I, I ). 

Clauses 

power (_,0,P,P):-!.

 power ( X,Y,P,R):- P1= P*X, Y1 =Y-1, power(X,Y1,P1,R). 

 Goal power(3,2,1,R). 

Output

 R= 9

Example8: program without using cut. 

sol//

Domains

 I= integer 

Predicates 

 no( I ) 

Clauses 

no (5):-!.

 no (7). 

 no (10).

 Goal no (X). 

Output: 

 X=5


Example9: program without using cut.

sol//

Domains 

 I =integer 

 S = symbol 

Predicates 

 a (I )

 b (s ) 

 c ( I, s )

 Clauses 

 a(10).

 a(20) 

 b(a) 

 b(c)

 c (X,Y):- a(X), b(Y).

 Goal c(X,Y). 

Output:

 X= 10 Y=a 

 X=10 Y=c 

 X=20 Y=a 

 X=20 Y=c 

Example 10: using cut at the end of the rule.

sol//

Domains 

 I =integer 

 S = symbol 

Predicates

 a(I )

 b (s ) 

 c (I, s ) 

Clauses 

 a(10). 

 a(20) 

 b(a) 

 b(c) 

 c (X, Y):- a (X), b (Y),!. 

Goal c(X,Y). 

Output: 

 X= 10 Y=a

Example 11: using cut at the middle of the rule.

sol//

Domains 

 I =integer 

 S = symbol 

Predicates 

 a(I )

 b (s )

 c ( I, s )

 Clauses

 a(10).

 a(20)

 b(a) 

 b(c) 

 c (X,Y):- a(X),!, b (Y). 

Goal c(X,Y). 

Output: 

 X=10 Y=a 

         Y=c 

Example 12: Calculate  counter from 1-10.

sol//

domains

 i=integer 

predicates 

counter(i) 

 counter(10):-!. 

counter(X):-write(X), X1=X+1, counter(X1). 

Goal: counter(1) 

Output: 1 2 3 4 5 6 7 8 9


Example 13: Calculate  summation of 10 numbers

sol//

predicates 

sum(integer,integer,integer) 

clauses

 sum(10,S,S):-!.

 sum(X,Y,S):-Y1=Y+X,X1=X+1,sum(X1,Y1,S),!. 

Goal: sum(1,0,M) 

Output: ? 


Example 14: Calculate  summation of 10 given integer numbers.

sol//

domains 

int=integer 

predicates 

sum_int(int,int,int) 

clauses sum_int(10,S,S):-!.

 sum_int(X,Y,S):-readint(Z),Z>0, X1=X+1,Y1=Y+Z,sum_int(X1,Y1,S),!.

 sum_int(X,Y,S):-X1=X+1,sum_int(X1,Y,S),!. 

Goal: sum_int(1,0,M) 

Output: ? 

Example 15: Print the contents of a list.

sol//

domains 

ilist=integer* 

predicates 

print(ilist) 

clauses

 print([]).

 print([X|T]):-write(X),print(T). 

Goal: print([3,4,5])

 Output: 3 4 5 yes

Example 16: Program to find sum of integer list.

sol//

Domains

 I= integer

 L=i* 

Predicates 

 Sum ( L ,I, I)

 Clauses Sum ( [ ],S,S):-!. 

Sum( [H| T],S1,S):- S2 = S1+H , Sum (T,S2,S).

 Goal Sum ( [ 1,4,6,9],0 ,S).

 Output S = 20 yes

Example 17: Prolog program to spilt list into positive list and negative list.

sol//

 Domains 

L= integer*

 Predicates 

Spilt ( L,L,L)

 Clauses Spilt ( [ ],[ ],[ ]):-!. 

Spilt ( [ H| T],[H|T1],L2):- H>= 0,!,spilt (T, T1,L2).

 Spilt ([H|T],L1,[H|T2] ) :- spilt ( T,L1,T2). 

Goal Spilt ([-1,4,-9,8,0],L1,L2). 

Output

. L1 = [4,8,0]

 L2 = [-1,-9]

Example 18:Find the maximum value of the list.

sol//

domains 

ilist=integer*

 predicates 

list(ilist,integer) 

clauses

 list([H],H).

 list([H1,H2|T],H):-H1>H2,list([H1|T],H). 

list([_,H2|T],H):-list([H2|T],H). 

Goal: list([3,9,4,5],M)

 Output: 

M=9 yes

 [3,9,4,5]

 [9,4,5]

 [9,5] 

[9] 

Example 19:Append two lists.

sol//

domains

 ilist=integer* 

predicates 

app(ilist,ilist,ilist) 

clauses

 app([],L,L). 

app([X|L1],L2,[X|L3]) :-app(L1,L2,L3).

 Goal: app([3,4,5],[6,7,8],L)

 Output: L=[3,4,5,6,7,8] yes

Example 20:Write the contents of list inside at the given list

sol//

domains 

ilist=integer* 

lists=ilist* 

predicates

 list(lists)

 clauses

 list([]). 

list([[]]).

 list([[H|T]]):-write(H),list([T]). 

list([H|T]):-list([H]),list(T).

 Goal: list([[3,4,5],[6,7,8]]) 

Output: 3 4 5 6 7 8 yes


Example 21:Reveres the contents of the given list

sol//

domains

 slist=symbol* 

predicates 

rev(slist,slist) 

app(slist,slist,slist)

 clauses

 app([],X,X).

 app([H|T1],X,[H|T]):-app(T1,X,T). 

rev([X],[X]). rev([H|T],L):-rev(T,L1),app(L1,[H],L).

 Goal: rev([a,b,c,d],R) 

Output: R=[d,c,b,a]

Example 22:Delete a particular element from a list

sol//

domains 

i= integer 

l= i* 

Predicates 

Delall (i,l,l)

 Clauses

 Delall (_,[ ],[ ]):-!.

 Delall (X,[X|T1],T1) :-!.

 Delall (X,[ H|T1],[H|T2]):- Delall (X,T1,T2). 

Goal Delall(2,[1,3,2,3,2,1],X).

Output=?

Example 23:Finding the first element in a list (front).

sol//

s_list =symbol*

predicates

front(symbol , s_list )

clauses

front(E,[ E | _ ]).

goal:front(X,[ a,b,c].


.Example 24:Finding the last element in a list (front).

sol//

domains

s_list =symbol*

predicates

last(symbol , s_list )

clauses

last(E,[ E ]).

last( E , [ _ | T ] ):- member(E,T).

goal:last(X,[a,b,c]).

.Example 25:adding an element to a list .

sol//

 

s_list =symbol*

predicates

add_first(symbol , s_list,s_list )

clauses

add_first(E,L,[ E| L ]).

output

goal:add_first(ban,[ahmed,noor],R)

R=[ban,ahmed,noor]

1solution

.Example 26:adding element to the end of a list.

sol//

s_list =symbol*

predicates

add_last(symbol , s_list,s_list )

clauses

add_last(E,[ ] ,[ E ]).

add_last( E , [ H| T1], [ H|T2]):-add_last(E , T1,T2).

output

goal:add_last(ban,[ahmed,noor],R)

R=[ahmed,noor,ban]

1solution

.Example 27:Delete the first occurrence element from a list

sol//

 

s_list =symbol*

predicates

delete_first(symbol , s_list,s_list )

clauses

delete_first(E, [E|L], L ) :-! .

delete_first(E, [H|T1],[H|T2]):-delete_first (E,T1,T2).

output

goal:delete_first(ban,[ahmed,ban,noor,ban],R)

R=[ahmed,noor,ban]

1solution.

.Example 28:Deleting all occurrence of an element from a list

sol//

s_list =symbol*

predicates

delete_all(symbol , s_list,s_list )

clauses

delete_all( _ , [ ] , [ ] ):-! .

delete_all(E, [E|T1],T2):-! , delete_all(E,T1,T2).

delete_all(E,[H|T1],[H|T2]):-delete_first(E,T1,T2).

output

goal:delete_all(ban,[ahmed,ban,noor,ban],R)

R=[ahmed,noor]

1 solution

example 29/


sol/




تعليقات



حجم الخط
+
16
-
تباعد السطور
+
2
-