Welcome to W3Courses
 Like Us on Facebook

Prolog

Appends Two Lists using Prolog Source Code

The following code appends two lists

CODE
append([],Y,Y).
append([X|T],Y,[X|R]) :- append(X,Y,R).

RESULT
?- append([a],[b,c],R).
R = [a, b, c]
Yes
 

Share

Delete all occurrences of an Element from a List using Prolog Source Code

The following code deletes all occurrences of an element from a list

CODE
deleteall([],A,[]).
deleteall([H|T],A,Result) :- H=A, deleteall(T,A,Result).
deleteall([H|T],A,[H|Result]) :- deleteall(T,A,Result).


RESULT
?- deleteall([a,b,a,c],a,Result).
Result = [b, c]
Yes


 

Share

Delete the Nth Element from the List using Prolog Source Code

The following code deletes the Nth element from the list

delN([],A,X,N,[]).
delN([H|T],A,X,N,Result) :- H=A, N=1, delN(T,A,X,X,Result).
delN([H|T],A,X,N,[H|Result]) :- H=A, N>1, N1 is N-1, delN(T,A,X,N1,Result).
delN([H|T],A,X,N,[H|Result]) :- delN(T,A,X,N,Result).

Share

List Length Function using Prolog Source Code

The following code returns the length of a given list

CODE
length([],0).
length([H|T],R+1) :- length(T,R).


RESULT
?- length([a,b,c],Z).
Z = 3
Yes

 


 

Share

List Member Function using Prolog Source Code

The following code checks if an element is a member of a given list

CODE
member(X, [X|Y]).
member(X, [H|L]) :- member(X, L).


RESULT
?- member(b, [a,b,c]).
Yes

Share

List Merge Function using Prolog Source Code

The following code merges two list in ascending order.

 merge([],Ys,Ys).
 merge(Xs,[],Xs).
 merge([X|Xs],[Y|Ys],[X|R]):- X<Y,merge(Xs,[Y|Ys],R).
 merge([X|Xs],[Y|Ys],[Y|R]):-X>Y,merge([X|Xs],Ys,R).
 merge([X|Xs],[X|Ys],[X,X|R]):-merge(Xs,Ys,R).

Share

MergeSort Function using Prolog Source Code

The following code merges and sorts two lists

mergesort([],[]).
mergesort([X],[X]).
mergesort([X,Y|Xs],Ys) :- split([X,Y|Xs],Xs1,Xs2), mergesort(Xs1,Ys1), mergesort(Xs2,Ys2), merge(Ys1,Ys2,Ys).

split([],[],[]).
split([X|Xs],[X|Ys],Zs) :- split(Xs,Zs,Ys).

merge([],Xs,Xs).
merge(Xs,[],Xs).
merge([X|Xs],[Y|Ys],[Z|Zs]) :- ( X =< Y -> Z = X, merge(Xs,[Y|Ys],Zs) ; Z = Y, merge([X|Xs],Ys,Zs)).

Share

Powersort Function using Prolog Source Code

The following code sorts lists

subset([],[]).
subset(S, [|T]) :- subset(S,T).
subset([H|S],[H|T]) :- subset(S,T).
powerset(Set,PowerSet) :- bagof(S,subset(S,Set),Powerset).
num(X,[X|T],T).
num(X,[H|T],[H|B]) :- num(X,T,B).
subset([],_).
subset([H|T],A) :- num(H,A,B), subset(T,B).
powerset(Set,Powerset) :- setoff(S, subset(S,Set), Powerset).

Share

QuickSort Function using Prolog Source Code

Quick Sort code:

gtq(X,Y) :- X @> Y.  

quicksort( [],[] ).

quicksort( [X | Tail], Sorted) :-
        split( X, Tail, Small, Big),
        quicksort( Small, SortedSmall),
        quicksort( Big, SortedBig),
        conc( SortedSmall, [X | SortedBig], Sorted).

split( _, [], [], []).

Share

Replace Function using Prolog Source Code

The following code replaces an element with a new element in the list

 CODE
 replace([],A,B,[]).
 replace([H|T],A,B,[B|Result]) :- H=A, replace(T,A,B,Result).
 replace([H|T],A,B,[H|Result]) :- replace(T,A,B,Result).


 RESULT
 ?- replace([a,b,a,c,a,d],a,w,R).
 R = [w, b, w, c, w, d]
 Yes

Share