Flatten a list in Prolog

The definition of flatten2/2 you’ve given is busted; it actually behaves like this: ?- flatten2([a, [b,c], [[d],[],[e]]], R). R = [a, b, c] ; false. So, given the case where you’ve already bound R to [a,b,c,d,e], the failure isn’t surprising. Your definition is throwing away the tail of lists (ListTail) in the 3rd clause – … Read more