Solution
%% The empty list is already flat. myflatten([],[]). %% For a non-empty list: flatten the head and flatten the tail and %% append the results. myflatten([Head|InTail],Out) :- myflatten(Head,FlatHead), myflatten(InTail,OutTail), append(FlatHead,OutTail,Out). %% When trying to flatten the head, you might find that the head is %% not itself a list. In this case the head of the input list simply %% becomes the head of the output list. myflatten([Head|InTail], [Head|OutTail]) :- Head \= [], Head \= [_|_], myflatten(InTail,OutTail).