Tuesday, 26 August 2014

Prolog program to find the last element of the list

Prolog program to find the last element of the list

Program:

domains
    list=symbol*
    
predicates
    last(list)

clauses
    last([X]):- write("\nLast element is :" ,X).
    last([Y|Tail]):-last(Tail).
    

goal with output

goal: replace([a,b,c,d,a,b]).
output: Last element is :gTrue


Back | List Programs | Next

Thursday, 29 May 2014

List programs in Prolog

List Programs in Prolog

Sr. No.
Program
1
Prolog program to create a list of N numbers
2
Prolog program to print a list
3
4
5
6
7
8
9
10
Prolog program to replace a character with another character in the list
11
Prolog program to find the last element of the list
12
Prolog program to find the Nth element of the list
13
Prolog program to find the union of two lists
14
Prolog program to find the intersection of two lists
15
Prolog program to find the set difference of two lists
16
Prolog program to check whether the first list is subset of another list or not
17
Prolog program to check whether the two lists are equal or not
18
Prolog program to check whether the two lists are equivalent or not
19
Prolog program to check whether the two lists are same or not
20
Prolog program to check whether the first list is sublist (in same order) of the other list
21
Prolog program to extract first N characters from the list
22
Prolog program to extract a sublist from a list given two indexes
23
Prolog program to extract last N characters from the list
24
Prolog program to drop the first N characters and extract the remaining list
25
Prolog program to spilt a list into a positive list numbers and a negative list numbers
26
Prolog program to remove the duplicate values from the list
27
Prolog program to split the list in two lists of same length (±1)
28
Prolog program to find all the permutations of the elements of the list
29
Prolog program to find the maximum value of the list
30
Prolog program to find the minimum value of the list
31
Prolog program to find the sum of the values of the list
32
Prolog program that take two lists as input and produce a third list as output, this list is the sum of the two lists

Tuesday, 27 May 2014

Prolog Program to replace a character with another character in the List.

Prolog Program to replace a character with another character in the List.

Program:

domains
list=symbol*
 
predicates
replace(symbol,symbol,list,list) //first symbol is replaced by second symbol

clauses
replace(_,_,[],[]).
replace(X,Y,[X|T],[Y|NT]):-replace(X,Y,T,NT).
replace(X,Y,[H|T],[H|NT]):-H<>X,replace(X,Y,T,NT).
    

goal with output

goal: replace(a,c,[a,b,c,d,a,b],X)
output: X=["c","b","c","d","c","b"]
           1 solution


BackList Programs | Next

Prolog Program to reverse the List.

Prolog Program to reverse the List.

Program:

domains    
list=integer*

predicates
reverse(list,list).
append(list,list,list).

clauses
reverse([],[]).
reverse([H|T1],T2):-reverse(T1,T3),append(T3,[H],T2).
append([],L,L).
append([X|L1],L2,[X|L3]):-append(L1,L2,L3).

goal with output

goal: reverse([1,2,3,4],X)
output: X=[4,3,2,1]
           1 solution


Prolog Program to delete all the occurrences of an element from the List.

Prolog Program to delete all the occurrences of an element from the List.

Program:

domains
list=symbol*

predicates
deleteall(symbol,list,list)

clauses
deleteall(_,[],[]).
deleteall(X,[X|T],NT):-deleteall(X,T,NT).
deleteall(X,[H|T],[H|NT]):-H<>X,deleteall(X,T,NT).

goal with output

goal: deleteall(b,[a,b,c,a,b],X)
output: X=["a","c","a"]
           1 solution


Prolog Program to delete an element from the list.

Prolog Program to delete an element from the list.

Program:

domains
list=symbol*

predicates
del(symbol,list,list)
 
clauses
del(X,[X|Tail],Tail).
del(X,[Y|Tail],[Y|Tail1]) :- del(X,Tail,Tail1).

goal with output

goal: del(b,[a,b,c,a,b],X)
output: X=["a","c","a","b"]
           X=["a","b","c","a"]
           2 solution


Prolog Program to append/concatenate two Lists.

Prolog Program to append/concatenate two Lists.

Program:

domains    
list=integer*

predicates
 append(list,list,list).

clauses
 append([],L,L).
 append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).

goal with output

goal: append([1,2,3,4],[5,6,7,8,9],X)
output: X=[1,2,3,4,5,6,7,8,9]
           1 solution