I'm working on a problem to flatten only one level of a list in Prolog. For example, [[1],[2,3]]
would become [1,2,3]
, but [[1,[2]],3]
would only flatten down to [1,[2],3]
. I went through some other questions on the site, but none thoroughly answered this question, and I just can't get my code to work on all my test cases.
Update: the code works! Here is the eventual answer that I came to:
my_flatten([], []).
my_flatten([A|B],L) :- is_list(A), my_flatten(B,B1), !, append(A,B1,L).
my_flatten([A|B],[A|B1]) :- my_flatten(B,B1).