Friday, October 26, 2012

Renaissance - YMCA University News letter

YMCA University of Sc. & Tech. is coming out with 2nd edition of its news letter called "Renaissance" It is edited by Prof. Divya Jyoti Singh. Its link is given below:

www.renaissanceymcaust.blogspot.com

Monday, October 22, 2012

Some Important PROLOG Predicates

Some popular and important PROLOG predicates are given below for the readers. These were discussed in a class of Artificial Intelligence.



1. /*Sum of a list of numbers*/
domains
list=integer*
predicates
sum(list,integer)
clauses
sum([],0).
sum([H|T],R):-sum(T,S),R=S+H.

2. /*Compare 2 Lists of numbers*/
domains
list=integer*
predicates
ifEqual(list,list)
clauses
ifEqual([],[]).
ifEqual([H1|T1],[H2|T2]):-H1=H2,ifEqual(T1,T2).

3. /*Find the last element of a list*/
domains
list=integer*
predicates
last(list,integer)
clauses
last([X],X).
last([H|T],L):-last(T,L).

4. /*delete last the last element of a list */
domains
list=integer*
predicates
deleteLast(list,list)
clauses
deleteLast([X],[]).
deleteLast([H|T],[H|NT]):-deleteLast(T,NT).


5. /*Separate positive and negative numbers from a list and put them in two separate lists*/
domains
list=integer*
predicates
separate(list,list,list)
clauses
separate([],[],[]).
separate([H|T],[H|PL],NL):-H>0,separate(T,PL,NL).
separate([H|T],PL,[H|NL]):-H<0 br="br" separate="separate">
6. /*Partition a list into two lists lessX and GreatX which contains elements less than X and greater than X respectively*/
domains
list=integer*
predicates
partition(integer,list,list,list)
clauses
partition(X,[],[],[]).
partition(X,[H|T],[H|LX],GX):-H<=X,partition(X,T,LX,GX).
partition(X,[H|T],LX,[H|GX]):-H>X,partition(X,T,LX,GX).

7. /*Find the smallest in a given list*/
domains
list=integer*
predicates
smallest(list,integer)
clauses
smallest([X],X).
smallest([H|T],X):-smallest(T,X),X smallest([H|T],H):-smallest(T,X),H
8. /*Find wheather a given number X is a member of a list or not */
domains
list=integer*
predicates
member(integer,list)
clauses
member(X,[X]).
member(X,[H|T]):-X=H.
member(X,[H|T]):-X<>H,member(X,T).

9. /*Create union of two given lists*/
domains
list=integer*
predicates
member(integer,list)
union(list,list,list)
clauses
member(X,[X]).
member(X,[H|T]):-X=H.
member(X,[H|T]):-X<>H,member(X,T).
union([],L2,L2).
union(L1,[],L1).
union([H|T],L2,[H|NL]):-NOT(member(H,L2)),union(T,L2,NL).
union([H|T],L2,NL):-member(H,L2),union(T,L2,NL).

10. /* Create intersection of two given lists*/
domains
list=integer*
predicates
member(integer,list)
intersection(list,list,list)
clauses
member(X,[X]).
member(X,[H|T]):-X=H.
member(X,[H|T]):-X<>H,member(X,T).
intersection([],L2,[]).
intersection(L1,[],[]).
intersection([H|T],L2,NL):-NOT(member(H,L2)),intersection(T,L2,NL).
intersection([H|T],L2,[H|NL]):-member(H,L2),intersection(T,L2,NL).

11. /*Give ListA minus ListB*/
domains
list=integer*
predicates
member(integer,list)
AminusB(list,list,list)
clauses
member(X,[X]).
member(X,[H|T]):-X=H.
member(X,[H|T]):-X<>H,member(X,T).
AminusB([],L2,[]).
AminusB(L1,[],L1).
AminusB([H|T],L2,[H|NL]):-NOT(member(H,L2)),AminusB(T,L2,NL).
AminusB([H|T],L2,NL):-member(H,L2),AminusB(T,L2,NL).

12. /*concatenate two lists */
domains
list=integer*
predicates
concatenation(list,list,list)
clauses
concatenation([],L2,L2).
concatenation(L1,[],L1).
concatenation([H|T],L2,[H|NL]):-concatenation(T,L2,NL).

13. /*replace X by Y of all occurrences of X in a list*/
domains
list=integer*
predicates
replace(integer,integer,list,list)
clauses
replace(X,Y,[],[]).
replace(X,Y,[H|T],[Y|NL]):-H=X,replace(X,Y,T,NL).
replace(X,Y,[H|T],[H|NL]):-H<>X,replace(X,Y,T,NL).

14. /*Reverse a given list */
domains
list=integer*
predicates
Reverse(list,list)
append(list,list,list)
clauses
append([],L2,L2).
append(L1,[],L1).
append([H|T],L2,[H|NL]):-append(T,L2,NL).
Reverse([],[]).
Reverse([H|T],NL):-Reverse(T,RL),append(RL,[H],NL).

15. /*Determine whether a list is palindrome or not*/
domains
list=integer*
predicates
palindrome(list)
last(list,integer)
deleteLast(list,list)
clauses
last([X],X).
last([H|T],L):-last(T,L).
deleteLast([X],[]).
deleteLast([H|T],[H|NT]):-deleteLast(T,NT).
palindrome([]).
palindrome([X]).
palindrome([H|T]):-last(T,L),H=L,deleteLast(T,NT),palindrome(NT).

16. /*merge two co sequential lists into a third list*/
domains
list=integer*
predicates
merge(list,list,list)
clauses
merge([],L2,L2).
merge(L1,[],L1).
merge([H1|T1],[H2|T2],[H1|NL]):-H1<=H2,merge(T1,[H2|T2],NL).
merge([H1|T1],[H2|T2],[H2|NL]):-H2


17. / *Quick sort a given list of numbers*/

domains
list=integer*
predicates
partition(integer,list,list,list)
append(list,list,list)
quickSort(list,list)
clauses
partition(X,[],[],[]).
partition(X,[H|T],[H|LX],GX):-H<=X,partition(X,T,LX,GX).
partition(X,[H|T],LX,[H|GX]):-H>X,partition(X,T,LX,GX).
append(L1,[],L1).
append([],L2,L2).
append([H|T],L2,[H|NT]):-append(T,L2,NT).
quickSort([],[]).
quickSort([H|T],FL):-partition(H,T,LH,GH),quickSort(LH,LSL),quickSort(GH,GSL),append(LSL,[H|GSL],FL).


18. /*merge sort a given list of numbers*/
domains
list=integer*
predicates
merge(list,list,list)
divide(list,list,list)
append(list,list,list)
mergeSort(list,list)
last(list,integer)
deleteLast(list,list)
clauses
last([X],X).
last([H|T],L):-last(T,L).
deleteLast([X],[]).
deleteLast([H|T],[H|NT]):-deleteLast(T,NT).
divide([],[],[]).
divide([H|T],[H|NT1],NL):-last([H|T],L),deleteLast(T,NT),divide(NT,NT1,NT2),append(NT2,[L],NL).
merge([],L2,L2).
merge(L1,[],L1).
merge([H1|T1],[H2|T2],[H1|NL]):-H1<=H2,merge(T1,[H2|T2],NL).
merge([H1|T1],[H2|T2],[H2|NL]):-H2


append(L1,[],L1).
append([],L2,L2).
append([H|T],L2,[H|NT]):-append(T,L2,NT).
mergeSort([],[]).
mergeSort([H],[H]).
mergeSort(L,FL):-d(L,L1,L2),mergeSort(L1,SL1),mergeSort(L2,SL2),merge(SL1,SL2,FL).

19. /* Sort a given list of numbers using selection sort*/
domains
list=integer*
predicates
selectionSort(list,list)
smallest(list,integer)
replace(integer,integer,list,list)
clauses
smallest([X],X).
smallest([H|T],X):-smallest(T,X),X smallest([H|T],H):-smallest(T,X),H replace(X,Y,[],[]).
replace(X,Y,[H|T],[Y|T]):-H=X.
replace(X,Y,[H|T],[H|NT]):-H<>X,replace(X,Y,T,NT).
selectionSort([],[]).
selectionSort([H],[H]).
selectionSort([H|T],[S|SL]):-smallest(T,S),H>S,replace(S,H,T,RL),selectionSort(RL,SL).
selectionSort([H|T],[H|SL]):-smallest(T,S),S>H,selectionSort(T,SL).

20. /* Sort a given list of numbers using insertion sort*/
domains
list=integer*
predicates
insert(integer,list,list)
append(list,list,list)
insertionSort(list,list)
clauses
insert(X,[],[X]).
insert(X,[H|T],NL):-X<=H,append([X],[H|T],NL).
insert(X,[H|T],[H|NT]):-X>H,insert(X,T,NT).
append(L1,[],L1).
append([],L2,L2).
append([H|T],L2,[H|NT]):-append(T,L2,NT).
insertionSort([],[]).
insertionSort([H|T],SL):-insertionSort(T,NT),insert(H,NT,SL).




Thought to ponder....

The sanctification of the five senses is the way to truth.

Bhagwan Sri Satya Sai Baba