Home > Net >  Insert zeros in a list on Prolog
Insert zeros in a list on Prolog

Time:03-18

I need to insert zeros after each element of a given list in prolog, could you help me please, I can't find the right way to make it work. This is my code:

insert_zero([A|T]),([A|B]):- 
   insert_zero([Rest|R1]), append(0,R1,B).

The answer should give me this

?- insert_zero([a,2,c,3], R). 
R = [a,0,2,0,c,0,3,0].

CodePudding user response:

This can be implemented with a simple predicate:

insert_zero([],[]).
insert_zero([H|T],[H,0|T1]):-
    insert_zero(T,T1).

?- insert_zero([a,2,c,3], R).
R = [a, 0, 2, 0, c, 0, 3, 0].
  • Related