Welcome to W3Courses
 Like Us on Facebook

List Merge

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