Put an element in front of each element of a given list. ?- inser(a,1,[2,1,3,4],R). R = [2,a,1,3,4] ?- append(X,Y,[a,b]). X = [] Y = [a,b] X = [a,b] Y = [] X = [a] Y = [b]
Solution 1: inser(X,A,[],[]). inser(X,A,[A|T],[X,A|T]):-!. inser(X,A,[H|T],[H|R]):-inser(X,A,T,R).
Solution 2: inser(X,A,L,R):- append(P1,[A|P2],L),append(P1,[X,A|P2],R). append([],L,L). append([H|T],L,[H|R]):- append(T,L,R).