I want to write a predicate that will insert a given element after every element in a list and will print resulting list. I want to use recurrsion for this.
For example: my list is [1,2,3,4,5]
and I want to insert 0
after every element. So the resulting list should be: [1,0,2,0,3,0,4,0,5,0]
.
I wrote this:
add([],X).
add([H|T],[H|[X|T1]]):-
add(T,T1).
add([H|T],[H|T1]):-
add(T,T1).
And I run this like: add([1,2,3], 0).
but Prolog says only No
. What does it mean? How can I improve this? Thanks
Edit:
It works, but shows some warnings about singleton variables:
add([], X, []).
add([H|T], X, [H|[X|T1]]):- add(T, X, T1).
add([H|T], X, [H|T1]):- add(T, X, T1).