This is my code and it doesnt work.
skip([], []).
skip([H|T], [H|R]):-
atomic(H),
!,
skip(T, R).
skip([_|T], R):-
skip(T, R).
tails([], []).
tails([H|T], R]):-
atomic(H),
!,
skip(T, T1),
tails(T1, R).
tails([H|T], [H|R]):-
tails(H, R1),
skip(T, T1),
tails(T1, R2),
append(R1, R2, R).
If we input list L = [1, [2, 3, [4, 5] ] ] the result should be R = [1, 3, 5].
not sure I understood your requirements, but this code satisfies your sample.
test: